fetch: centralize logic to print remote URL
[alt-git.git] / usage.c
blob5a7c6c346c14ca1fb03b99b8ee36574226cf3fd4
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 static 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 void die_message_builtin(const char *err, va_list params)
60 trace2_cmd_error_va(err, params);
61 vreportf(_("fatal: "), err, params);
65 * We call trace2_cmd_error_va() in the below functions first and
66 * expect it to va_copy 'params' before using it (because an 'ap' can
67 * only be walked once).
69 static NORETURN void die_builtin(const char *err, va_list params)
71 report_fn die_message_fn = get_die_message_routine();
73 die_message_fn(err, params);
74 exit(128);
77 static void error_builtin(const char *err, va_list params)
79 trace2_cmd_error_va(err, params);
81 vreportf(_("error: "), err, params);
84 static void warn_builtin(const char *warn, va_list params)
86 trace2_cmd_error_va(warn, params);
88 vreportf(_("warning: "), warn, params);
91 static int die_is_recursing_builtin(void)
93 static int dying;
95 * Just an arbitrary number X where "a < x < b" where "a" is
96 * "maximum number of pthreads we'll ever plausibly spawn" and
97 * "b" is "something less than Inf", since the point is to
98 * prevent infinite recursion.
100 static const int recursion_limit = 1024;
102 dying++;
103 if (dying > recursion_limit) {
104 return 1;
105 } else if (dying == 2) {
106 warning("die() called many times. Recursion error or racy threaded death!");
107 return 0;
108 } else {
109 return 0;
113 /* If we are in a dlopen()ed .so write to a global variable would segfault
114 * (ugh), so keep things static. */
115 static NORETURN_PTR report_fn usage_routine = usage_builtin;
116 static NORETURN_PTR report_fn die_routine = die_builtin;
117 static report_fn die_message_routine = die_message_builtin;
118 static report_fn error_routine = error_builtin;
119 static report_fn warn_routine = warn_builtin;
120 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
122 void set_die_routine(NORETURN_PTR report_fn routine)
124 die_routine = routine;
127 report_fn get_die_message_routine(void)
129 return die_message_routine;
132 void set_error_routine(report_fn routine)
134 error_routine = routine;
137 report_fn get_error_routine(void)
139 return error_routine;
142 void set_warn_routine(report_fn routine)
144 warn_routine = routine;
147 report_fn get_warn_routine(void)
149 return warn_routine;
152 void set_die_is_recursing_routine(int (*routine)(void))
154 die_is_recursing = routine;
157 void NORETURN usagef(const char *err, ...)
159 va_list params;
161 va_start(params, err);
162 usage_routine(err, params);
163 va_end(params);
166 void NORETURN usage(const char *err)
168 usagef("%s", err);
171 void NORETURN die(const char *err, ...)
173 va_list params;
175 if (die_is_recursing()) {
176 fputs("fatal: recursion detected in die handler\n", stderr);
177 exit(128);
180 va_start(params, err);
181 die_routine(err, params);
182 va_end(params);
185 static const char *fmt_with_err(char *buf, int n, const char *fmt)
187 char str_error[256], *err;
188 int i, j;
190 err = strerror(errno);
191 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
192 if ((str_error[j++] = err[i++]) != '%')
193 continue;
194 if (j < sizeof(str_error) - 1) {
195 str_error[j++] = '%';
196 } else {
197 /* No room to double the '%', so we overwrite it with
198 * '\0' below */
199 j--;
200 break;
203 str_error[j] = 0;
204 /* Truncation is acceptable here */
205 snprintf(buf, n, "%s: %s", fmt, str_error);
206 return buf;
209 void NORETURN die_errno(const char *fmt, ...)
211 char buf[1024];
212 va_list params;
214 if (die_is_recursing()) {
215 fputs("fatal: recursion detected in die_errno handler\n",
216 stderr);
217 exit(128);
220 va_start(params, fmt);
221 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
222 va_end(params);
225 #undef die_message
226 int die_message(const char *err, ...)
228 va_list params;
230 va_start(params, err);
231 die_message_routine(err, params);
232 va_end(params);
233 return 128;
236 #undef die_message_errno
237 int die_message_errno(const char *fmt, ...)
239 char buf[1024];
240 va_list params;
242 va_start(params, fmt);
243 die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
244 va_end(params);
245 return 128;
248 #undef error_errno
249 int error_errno(const char *fmt, ...)
251 char buf[1024];
252 va_list params;
254 va_start(params, fmt);
255 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
256 va_end(params);
257 return -1;
260 #undef error
261 int error(const char *err, ...)
263 va_list params;
265 va_start(params, err);
266 error_routine(err, params);
267 va_end(params);
268 return -1;
271 void warning_errno(const char *warn, ...)
273 char buf[1024];
274 va_list params;
276 va_start(params, warn);
277 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
278 va_end(params);
281 void warning(const char *warn, ...)
283 va_list params;
285 va_start(params, warn);
286 warn_routine(warn, params);
287 va_end(params);
290 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
291 int BUG_exit_code;
293 static void BUG_vfl_common(const char *file, int line, const char *fmt,
294 va_list params)
296 char prefix[256];
298 /* truncation via snprintf is OK here */
299 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
301 vreportf(prefix, fmt, params);
304 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
306 va_list params_copy;
307 static int in_bug;
309 va_copy(params_copy, params);
310 BUG_vfl_common(file, line, fmt, params);
312 if (in_bug)
313 abort();
314 in_bug = 1;
316 trace2_cmd_error_va(fmt, params_copy);
318 if (BUG_exit_code)
319 exit(BUG_exit_code);
320 abort();
323 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
325 va_list ap;
327 bug_called_must_BUG = 0;
329 va_start(ap, fmt);
330 BUG_vfl(file, line, fmt, ap);
331 va_end(ap);
334 int bug_called_must_BUG;
335 void bug_fl(const char *file, int line, const char *fmt, ...)
337 va_list ap;
339 bug_called_must_BUG = 1;
341 va_start(ap, fmt);
342 BUG_vfl_common(file, line, fmt, ap);
343 va_end(ap);
345 va_start(ap, fmt);
346 trace2_cmd_error_va(fmt, ap);
347 va_end(ap);
350 #ifdef SUPPRESS_ANNOTATED_LEAKS
351 void unleak_memory(const void *ptr, size_t len)
353 static struct suppressed_leak_root {
354 struct suppressed_leak_root *next;
355 char data[FLEX_ARRAY];
356 } *suppressed_leaks;
357 struct suppressed_leak_root *root;
359 FLEX_ALLOC_MEM(root, data, ptr, len);
360 root->next = suppressed_leaks;
361 suppressed_leaks = root;
363 #endif