Fourth batch
[git/raj.git] / usage.c
blob58fb5fff5f245c33898e27c85296a4a47325c34e
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)
84 vreportf("warning: ", warn, params);
87 static int die_is_recursing_builtin(void)
89 static int dying;
91 * Just an arbitrary number X where "a < x < b" where "a" is
92 * "maximum number of pthreads we'll ever plausibly spawn" and
93 * "b" is "something less than Inf", since the point is to
94 * prevent infinite recursion.
96 static const int recursion_limit = 1024;
98 dying++;
99 if (dying > recursion_limit) {
100 return 1;
101 } else if (dying == 2) {
102 warning("die() called many times. Recursion error or racy threaded death!");
103 return 0;
104 } else {
105 return 0;
109 /* If we are in a dlopen()ed .so write to a global variable would segfault
110 * (ugh), so keep things static. */
111 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
112 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
113 static void (*error_routine)(const char *err, va_list params) = error_builtin;
114 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
115 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
117 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
119 die_routine = routine;
122 void set_error_routine(void (*routine)(const char *err, va_list params))
124 error_routine = routine;
127 void (*get_error_routine(void))(const char *err, va_list params)
129 return error_routine;
132 void set_warn_routine(void (*routine)(const char *warn, va_list params))
134 warn_routine = routine;
137 void (*get_warn_routine(void))(const char *warn, va_list params)
139 return warn_routine;
142 void set_die_is_recursing_routine(int (*routine)(void))
144 die_is_recursing = routine;
147 void NORETURN usagef(const char *err, ...)
149 va_list params;
151 va_start(params, err);
152 usage_routine(err, params);
153 va_end(params);
156 void NORETURN usage(const char *err)
158 usagef("%s", err);
161 void NORETURN die(const char *err, ...)
163 va_list params;
165 if (die_is_recursing()) {
166 fputs("fatal: recursion detected in die handler\n", stderr);
167 exit(128);
170 va_start(params, err);
171 die_routine(err, params);
172 va_end(params);
175 static const char *fmt_with_err(char *buf, int n, const char *fmt)
177 char str_error[256], *err;
178 int i, j;
180 err = strerror(errno);
181 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
182 if ((str_error[j++] = err[i++]) != '%')
183 continue;
184 if (j < sizeof(str_error) - 1) {
185 str_error[j++] = '%';
186 } else {
187 /* No room to double the '%', so we overwrite it with
188 * '\0' below */
189 j--;
190 break;
193 str_error[j] = 0;
194 /* Truncation is acceptable here */
195 snprintf(buf, n, "%s: %s", fmt, str_error);
196 return buf;
199 void NORETURN die_errno(const char *fmt, ...)
201 char buf[1024];
202 va_list params;
204 if (die_is_recursing()) {
205 fputs("fatal: recursion detected in die_errno handler\n",
206 stderr);
207 exit(128);
210 va_start(params, fmt);
211 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
212 va_end(params);
215 #undef error_errno
216 int error_errno(const char *fmt, ...)
218 char buf[1024];
219 va_list params;
221 va_start(params, fmt);
222 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
223 va_end(params);
224 return -1;
227 #undef error
228 int error(const char *err, ...)
230 va_list params;
232 va_start(params, err);
233 error_routine(err, params);
234 va_end(params);
235 return -1;
238 void warning_errno(const char *warn, ...)
240 char buf[1024];
241 va_list params;
243 va_start(params, warn);
244 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
245 va_end(params);
248 void warning(const char *warn, ...)
250 va_list params;
252 va_start(params, warn);
253 warn_routine(warn, params);
254 va_end(params);
257 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
258 int BUG_exit_code;
260 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
262 char prefix[256];
264 /* truncation via snprintf is OK here */
265 if (file)
266 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
267 else
268 snprintf(prefix, sizeof(prefix), "BUG: ");
270 vreportf(prefix, fmt, params);
271 if (BUG_exit_code)
272 exit(BUG_exit_code);
273 abort();
276 #ifdef HAVE_VARIADIC_MACROS
277 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
279 va_list ap;
280 va_start(ap, fmt);
281 BUG_vfl(file, line, fmt, ap);
282 va_end(ap);
284 #else
285 NORETURN void BUG(const char *fmt, ...)
287 va_list ap;
288 va_start(ap, fmt);
289 BUG_vfl(NULL, 0, fmt, ap);
290 va_end(ap);
292 #endif
294 #ifdef SUPPRESS_ANNOTATED_LEAKS
295 void unleak_memory(const void *ptr, size_t len)
297 static struct suppressed_leak_root {
298 struct suppressed_leak_root *next;
299 char data[FLEX_ARRAY];
300 } *suppressed_leaks;
301 struct suppressed_leak_root *root;
303 FLEX_ALLOC_MEM(root, data, ptr, len);
304 root->next = suppressed_leaks;
305 suppressed_leaks = root;
307 #endif