doc/reftable: document how to handle windows
[git.git] / usage.c
blob1b206de36d6e1cb7799fce728c2a37e310479223
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "git-compat-util.h"
7 #include "cache.h"
9 void vreportf(const char *prefix, const char *err, va_list params)
11 char msg[4096];
12 char *p, *pend = msg + sizeof(msg);
13 size_t prefix_len = strlen(prefix);
15 if (sizeof(msg) <= prefix_len) {
16 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
17 abort();
19 memcpy(msg, prefix, prefix_len);
20 p = msg + prefix_len;
21 if (vsnprintf(p, pend - p, err, params) < 0)
22 *p = '\0'; /* vsnprintf() failed, clip at prefix */
24 for (; p != pend - 1 && *p; p++) {
25 if (iscntrl(*p) && *p != '\t' && *p != '\n')
26 *p = '?';
29 *(p++) = '\n'; /* we no longer need a NUL */
30 fflush(stderr);
31 write_in_full(2, msg, p - msg);
34 static NORETURN void usage_builtin(const char *err, va_list params)
36 vreportf("usage: ", err, params);
39 * When we detect a usage error *before* the command dispatch in
40 * cmd_main(), we don't know what verb to report. Force it to this
41 * to facilitate post-processing.
43 trace2_cmd_name("_usage_");
46 * Currently, the (err, params) are usually just the static usage
47 * string which isn't very useful here. Usually, the call site
48 * manually calls fprintf(stderr,...) with the actual detailed
49 * syntax error before calling usage().
51 * TODO It would be nice to update the call sites to pass both
52 * the static usage string and the detailed error message.
55 exit(129);
58 static NORETURN void die_builtin(const char *err, va_list params)
61 * We call this trace2 function first and expect it to va_copy 'params'
62 * before using it (because an 'ap' can only be walked once).
64 trace2_cmd_error_va(err, params);
66 vreportf("fatal: ", err, params);
68 exit(128);
71 static void error_builtin(const char *err, va_list params)
74 * We call this trace2 function first and expect it to va_copy 'params'
75 * before using it (because an 'ap' can only be walked once).
77 trace2_cmd_error_va(err, params);
79 vreportf("error: ", err, params);
82 static void warn_builtin(const char *warn, va_list params)
85 * We call this trace2 function first and expect it to va_copy 'params'
86 * before using it (because an 'ap' can only be walked once).
88 trace2_cmd_error_va(warn, params);
90 vreportf("warning: ", warn, params);
93 static int die_is_recursing_builtin(void)
95 static int dying;
97 * Just an arbitrary number X where "a < x < b" where "a" is
98 * "maximum number of pthreads we'll ever plausibly spawn" and
99 * "b" is "something less than Inf", since the point is to
100 * prevent infinite recursion.
102 static const int recursion_limit = 1024;
104 dying++;
105 if (dying > recursion_limit) {
106 return 1;
107 } else if (dying == 2) {
108 warning("die() called many times. Recursion error or racy threaded death!");
109 return 0;
110 } else {
111 return 0;
115 /* If we are in a dlopen()ed .so write to a global variable would segfault
116 * (ugh), so keep things static. */
117 static NORETURN_PTR report_fn usage_routine = usage_builtin;
118 static NORETURN_PTR report_fn die_routine = die_builtin;
119 static report_fn error_routine = error_builtin;
120 static report_fn warn_routine = warn_builtin;
121 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
123 void set_die_routine(NORETURN_PTR report_fn routine)
125 die_routine = routine;
128 void set_error_routine(report_fn routine)
130 error_routine = routine;
133 report_fn get_error_routine(void)
135 return error_routine;
138 void set_warn_routine(report_fn routine)
140 warn_routine = routine;
143 report_fn get_warn_routine(void)
145 return warn_routine;
148 void set_die_is_recursing_routine(int (*routine)(void))
150 die_is_recursing = routine;
153 void NORETURN usagef(const char *err, ...)
155 va_list params;
157 va_start(params, err);
158 usage_routine(err, params);
159 va_end(params);
162 void NORETURN usage(const char *err)
164 usagef("%s", err);
167 void NORETURN die(const char *err, ...)
169 va_list params;
171 if (die_is_recursing()) {
172 fputs("fatal: recursion detected in die handler\n", stderr);
173 exit(128);
176 va_start(params, err);
177 die_routine(err, params);
178 va_end(params);
181 static const char *fmt_with_err(char *buf, int n, const char *fmt)
183 char str_error[256], *err;
184 int i, j;
186 err = strerror(errno);
187 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
188 if ((str_error[j++] = err[i++]) != '%')
189 continue;
190 if (j < sizeof(str_error) - 1) {
191 str_error[j++] = '%';
192 } else {
193 /* No room to double the '%', so we overwrite it with
194 * '\0' below */
195 j--;
196 break;
199 str_error[j] = 0;
200 /* Truncation is acceptable here */
201 snprintf(buf, n, "%s: %s", fmt, str_error);
202 return buf;
205 void NORETURN die_errno(const char *fmt, ...)
207 char buf[1024];
208 va_list params;
210 if (die_is_recursing()) {
211 fputs("fatal: recursion detected in die_errno handler\n",
212 stderr);
213 exit(128);
216 va_start(params, fmt);
217 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
218 va_end(params);
221 #undef error_errno
222 int error_errno(const char *fmt, ...)
224 char buf[1024];
225 va_list params;
227 va_start(params, fmt);
228 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
229 va_end(params);
230 return -1;
233 #undef error
234 int error(const char *err, ...)
236 va_list params;
238 va_start(params, err);
239 error_routine(err, params);
240 va_end(params);
241 return -1;
244 void warning_errno(const char *warn, ...)
246 char buf[1024];
247 va_list params;
249 va_start(params, warn);
250 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
251 va_end(params);
254 void warning(const char *warn, ...)
256 va_list params;
258 va_start(params, warn);
259 warn_routine(warn, params);
260 va_end(params);
263 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
264 int BUG_exit_code;
266 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
268 char prefix[256];
269 va_list params_copy;
270 static int in_bug;
272 va_copy(params_copy, params);
274 /* truncation via snprintf is OK here */
275 if (file)
276 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
277 else
278 snprintf(prefix, sizeof(prefix), "BUG: ");
280 vreportf(prefix, fmt, params);
282 if (in_bug)
283 abort();
284 in_bug = 1;
286 trace2_cmd_error_va(fmt, params_copy);
288 if (BUG_exit_code)
289 exit(BUG_exit_code);
290 abort();
293 #ifdef HAVE_VARIADIC_MACROS
294 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
296 va_list ap;
297 va_start(ap, fmt);
298 BUG_vfl(file, line, fmt, ap);
299 va_end(ap);
301 #else
302 NORETURN void BUG(const char *fmt, ...)
304 va_list ap;
305 va_start(ap, fmt);
306 BUG_vfl(NULL, 0, fmt, ap);
307 va_end(ap);
309 #endif
311 #ifdef SUPPRESS_ANNOTATED_LEAKS
312 void unleak_memory(const void *ptr, size_t len)
314 static struct suppressed_leak_root {
315 struct suppressed_leak_root *next;
316 char data[FLEX_ARRAY];
317 } *suppressed_leaks;
318 struct suppressed_leak_root *root;
320 FLEX_ALLOC_MEM(root, data, ptr, len);
321 root->next = suppressed_leaks;
322 suppressed_leaks = root;
324 #endif