t5601: exercise clones with "includeIf.*.onbranch"
[alt-git.git] / usage.c
blob09f0ed509b79c5a162f06a7f4cb5c5296d36be56
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "git-compat-util.h"
7 #include "gettext.h"
8 #include "trace2.h"
10 static void vreportf(const char *prefix, const char *err, va_list params)
12 char msg[4096];
13 char *p, *pend = msg + sizeof(msg);
14 size_t prefix_len = strlen(prefix);
16 if (sizeof(msg) <= prefix_len) {
17 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
18 abort();
20 memcpy(msg, prefix, prefix_len);
21 p = msg + prefix_len;
22 if (vsnprintf(p, pend - p, err, params) < 0)
23 *p = '\0'; /* vsnprintf() failed, clip at prefix */
25 for (; p != pend - 1 && *p; p++) {
26 if (iscntrl(*p) && *p != '\t' && *p != '\n')
27 *p = '?';
30 *(p++) = '\n'; /* we no longer need a NUL */
31 fflush(stderr);
32 write_in_full(2, msg, p - msg);
35 static NORETURN void usage_builtin(const char *err, va_list params)
37 vreportf(_("usage: "), err, params);
40 * When we detect a usage error *before* the command dispatch in
41 * cmd_main(), we don't know what verb to report. Force it to this
42 * to facilitate post-processing.
44 trace2_cmd_name("_usage_");
47 * Currently, the (err, params) are usually just the static usage
48 * string which isn't very useful here. Usually, the call site
49 * manually calls fprintf(stderr,...) with the actual detailed
50 * syntax error before calling usage().
52 * TODO It would be nice to update the call sites to pass both
53 * the static usage string and the detailed error message.
56 exit(129);
59 static void die_message_builtin(const char *err, va_list params)
61 trace2_cmd_error_va(err, params);
62 vreportf(_("fatal: "), err, params);
66 * We call trace2_cmd_error_va() in the below functions first and
67 * expect it to va_copy 'params' before using it (because an 'ap' can
68 * only be walked once).
70 static NORETURN void die_builtin(const char *err, va_list params)
72 report_fn die_message_fn = get_die_message_routine();
74 die_message_fn(err, params);
75 exit(128);
78 static void error_builtin(const char *err, va_list params)
80 trace2_cmd_error_va(err, params);
82 vreportf(_("error: "), err, params);
85 static void warn_builtin(const char *warn, va_list params)
87 trace2_cmd_error_va(warn, params);
89 vreportf(_("warning: "), warn, params);
92 static int die_is_recursing_builtin(void)
94 static int dying;
96 * Just an arbitrary number X where "a < x < b" where "a" is
97 * "maximum number of pthreads we'll ever plausibly spawn" and
98 * "b" is "something less than Inf", since the point is to
99 * prevent infinite recursion.
101 static const int recursion_limit = 1024;
103 dying++;
104 if (dying > recursion_limit) {
105 return 1;
106 } else if (dying == 2) {
107 warning("die() called many times. Recursion error or racy threaded death!");
108 return 0;
109 } else {
110 return 0;
114 /* If we are in a dlopen()ed .so write to a global variable would segfault
115 * (ugh), so keep things static. */
116 static NORETURN_PTR report_fn usage_routine = usage_builtin;
117 static NORETURN_PTR report_fn die_routine = die_builtin;
118 static report_fn die_message_routine = die_message_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 report_fn get_die_message_routine(void)
130 return die_message_routine;
133 void set_error_routine(report_fn routine)
135 error_routine = routine;
138 report_fn get_error_routine(void)
140 return error_routine;
143 void set_warn_routine(report_fn routine)
145 warn_routine = routine;
148 report_fn get_warn_routine(void)
150 return warn_routine;
153 void set_die_is_recursing_routine(int (*routine)(void))
155 die_is_recursing = routine;
158 void NORETURN usagef(const char *err, ...)
160 va_list params;
162 va_start(params, err);
163 usage_routine(err, params);
164 va_end(params);
167 void NORETURN usage(const char *err)
169 usagef("%s", err);
172 void NORETURN die(const char *err, ...)
174 va_list params;
176 if (die_is_recursing()) {
177 fputs("fatal: recursion detected in die handler\n", stderr);
178 exit(128);
181 va_start(params, err);
182 die_routine(err, params);
183 va_end(params);
186 static const char *fmt_with_err(char *buf, int n, const char *fmt)
188 char str_error[256], *err;
189 int i, j;
191 err = strerror(errno);
192 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
193 if ((str_error[j++] = err[i++]) != '%')
194 continue;
195 if (j < sizeof(str_error) - 1) {
196 str_error[j++] = '%';
197 } else {
198 /* No room to double the '%', so we overwrite it with
199 * '\0' below */
200 j--;
201 break;
204 str_error[j] = 0;
205 /* Truncation is acceptable here */
206 snprintf(buf, n, "%s: %s", fmt, str_error);
207 return buf;
210 void NORETURN die_errno(const char *fmt, ...)
212 char buf[1024];
213 va_list params;
215 if (die_is_recursing()) {
216 fputs("fatal: recursion detected in die_errno handler\n",
217 stderr);
218 exit(128);
221 va_start(params, fmt);
222 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
223 va_end(params);
226 #undef die_message
227 int die_message(const char *err, ...)
229 va_list params;
231 va_start(params, err);
232 die_message_routine(err, params);
233 va_end(params);
234 return 128;
237 #undef die_message_errno
238 int die_message_errno(const char *fmt, ...)
240 char buf[1024];
241 va_list params;
243 va_start(params, fmt);
244 die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
245 va_end(params);
246 return 128;
249 #undef error_errno
250 int error_errno(const char *fmt, ...)
252 char buf[1024];
253 va_list params;
255 va_start(params, fmt);
256 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
257 va_end(params);
258 return -1;
261 #undef error
262 int error(const char *err, ...)
264 va_list params;
266 va_start(params, err);
267 error_routine(err, params);
268 va_end(params);
269 return -1;
272 void warning_errno(const char *warn, ...)
274 char buf[1024];
275 va_list params;
277 va_start(params, warn);
278 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
279 va_end(params);
282 void warning(const char *warn, ...)
284 va_list params;
286 va_start(params, warn);
287 warn_routine(warn, params);
288 va_end(params);
291 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
292 int BUG_exit_code;
294 static void BUG_vfl_common(const char *file, int line, const char *fmt,
295 va_list params)
297 char prefix[256];
299 /* truncation via snprintf is OK here */
300 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
302 vreportf(prefix, fmt, params);
305 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
307 va_list params_copy;
308 static int in_bug;
310 va_copy(params_copy, params);
311 BUG_vfl_common(file, line, fmt, params);
313 if (in_bug)
314 abort();
315 in_bug = 1;
317 trace2_cmd_error_va(fmt, params_copy);
319 if (BUG_exit_code)
320 exit(BUG_exit_code);
321 abort();
324 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
326 va_list ap;
328 bug_called_must_BUG = 0;
330 va_start(ap, fmt);
331 BUG_vfl(file, line, fmt, ap);
332 va_end(ap);
335 int bug_called_must_BUG;
336 void bug_fl(const char *file, int line, const char *fmt, ...)
338 va_list ap;
340 bug_called_must_BUG = 1;
342 va_start(ap, fmt);
343 BUG_vfl_common(file, line, fmt, ap);
344 va_end(ap);
346 va_start(ap, fmt);
347 trace2_cmd_error_va(fmt, ap);
348 va_end(ap);
351 #ifdef SUPPRESS_ANNOTATED_LEAKS
352 void unleak_memory(const void *ptr, size_t len)
354 static struct suppressed_leak_root {
355 struct suppressed_leak_root *next;
356 char data[FLEX_ARRAY];
357 } *suppressed_leaks;
358 struct suppressed_leak_root *root;
360 FLEX_ALLOC_MEM(root, data, ptr, len);
361 root->next = suppressed_leaks;
362 suppressed_leaks = root;
364 #endif