.mailmap: update email address of Andrey Mazo
[git/git.git] / usage.c
CommitLineData
0fcfd160
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
4050c0df 6#include "git-compat-util.h"
3bc4181f 7#include "cache.h"
0fcfd160 8
ebaa79f4 9void vreportf(const char *prefix, const char *err, va_list params)
0fcfd160 10{
b5a9e435 11 char msg[4096];
f2900898 12 char *p;
f4c3edc0 13
b5a9e435 14 vsnprintf(msg, sizeof(msg), err, params);
f2900898
JK
15 for (p = msg; *p; p++) {
16 if (iscntrl(*p) && *p != '\t' && *p != '\n')
17 *p = '?';
f4c3edc0 18 }
e3f43ce7 19 fprintf(stderr, "%s%s\n", prefix, msg);
3bc4181f
CB
20}
21
64b1cb74 22static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 23{
ebaa79f4 24 vreportf("usage: ", err, params);
ee4512ed
JH
25
26 /*
27 * When we detect a usage error *before* the command dispatch in
28 * cmd_main(), we don't know what verb to report. Force it to this
29 * to facilitate post-processing.
30 */
31 trace2_cmd_name("_usage_");
32
33 /*
34 * Currently, the (err, params) are usually just the static usage
35 * string which isn't very useful here. Usually, the call site
36 * manually calls fprintf(stderr,...) with the actual detailed
37 * syntax error before calling usage().
38 *
39 * TODO It would be nice to update the call sites to pass both
40 * the static usage string and the detailed error message.
41 */
42
5d1a5c02 43 exit(129);
0fcfd160
LT
44}
45
ce88ac5b 46static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 47{
ee4512ed
JH
48 /*
49 * We call this trace2 function first and expect it to va_copy 'params'
50 * before using it (because an 'ap' can only be walked once).
51 */
52 trace2_cmd_error_va(err, params);
53
ebaa79f4 54 vreportf("fatal: ", err, params);
ee4512ed 55
39a3f5ea
PB
56 exit(128);
57}
58
ce88ac5b 59static void error_builtin(const char *err, va_list params)
39a3f5ea 60{
ee4512ed
JH
61 /*
62 * We call this trace2 function first and expect it to va_copy 'params'
63 * before using it (because an 'ap' can only be walked once).
64 */
65 trace2_cmd_error_va(err, params);
66
ebaa79f4 67 vreportf("error: ", err, params);
39a3f5ea
PB
68}
69
fa39b6b5
SP
70static void warn_builtin(const char *warn, va_list params)
71{
ebaa79f4 72 vreportf("warning: ", warn, params);
fa39b6b5 73}
39a3f5ea 74
c19a490e
JK
75static int die_is_recursing_builtin(void)
76{
77 static int dying;
2d3c02f5
ÆAB
78 /*
79 * Just an arbitrary number X where "a < x < b" where "a" is
80 * "maximum number of pthreads we'll ever plausibly spawn" and
81 * "b" is "something less than Inf", since the point is to
82 * prevent infinite recursion.
83 */
84 static const int recursion_limit = 1024;
85
86 dying++;
87 if (dying > recursion_limit) {
88 return 1;
89 } else if (dying == 2) {
90 warning("die() called many times. Recursion error or racy threaded death!");
91 return 0;
92 } else {
93 return 0;
94 }
c19a490e
JK
95}
96
39a3f5ea
PB
97/* If we are in a dlopen()ed .so write to a global variable would segfault
98 * (ugh), so keep things static. */
64b1cb74 99static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 100static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 101static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 102static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
c19a490e 103static int (*die_is_recursing)(void) = die_is_recursing_builtin;
39a3f5ea 104
18660bc9 105void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
106{
107 die_routine = routine;
108}
109
3bc4181f
CB
110void set_error_routine(void (*routine)(const char *err, va_list params))
111{
112 error_routine = routine;
113}
114
725149be
CC
115void (*get_error_routine(void))(const char *err, va_list params)
116{
117 return error_routine;
118}
119
b83f108b
CC
120void set_warn_routine(void (*routine)(const char *warn, va_list params))
121{
122 warn_routine = routine;
123}
124
725149be
CC
125void (*get_warn_routine(void))(const char *warn, va_list params)
126{
127 return warn_routine;
128}
129
c19a490e
JK
130void set_die_is_recursing_routine(int (*routine)(void))
131{
132 die_is_recursing = routine;
133}
134
c2e86add 135void NORETURN usagef(const char *err, ...)
64b1cb74
JN
136{
137 va_list params;
138
139 va_start(params, err);
140 usage_routine(err, params);
141 va_end(params);
142}
143
c2e86add 144void NORETURN usage(const char *err)
39a3f5ea 145{
64b1cb74 146 usagef("%s", err);
39a3f5ea
PB
147}
148
c2e86add 149void NORETURN die(const char *err, ...)
0fcfd160
LT
150{
151 va_list params;
152
c19a490e 153 if (die_is_recursing()) {
cd163d4b
BC
154 fputs("fatal: recursion detected in die handler\n", stderr);
155 exit(128);
156 }
cd163d4b 157
0fcfd160 158 va_start(params, err);
39a3f5ea 159 die_routine(err, params);
0fcfd160 160 va_end(params);
0fcfd160
LT
161}
162
58e4e511 163static const char *fmt_with_err(char *buf, int n, const char *fmt)
b875036e 164{
f8b5a8e1
JH
165 char str_error[256], *err;
166 int i, j;
167
168 err = strerror(errno);
169 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
170 if ((str_error[j++] = err[i++]) != '%')
171 continue;
172 if (j < sizeof(str_error) - 1) {
173 str_error[j++] = '%';
174 } else {
175 /* No room to double the '%', so we overwrite it with
176 * '\0' below */
177 j--;
178 break;
179 }
180 }
181 str_error[j] = 0;
ac4896f0 182 /* Truncation is acceptable here */
58e4e511
NTND
183 snprintf(buf, n, "%s: %s", fmt, str_error);
184 return buf;
185}
186
187void NORETURN die_errno(const char *fmt, ...)
188{
189 char buf[1024];
190 va_list params;
191
192 if (die_is_recursing()) {
193 fputs("fatal: recursion detected in die_errno handler\n",
194 stderr);
195 exit(128);
196 }
b875036e
TR
197
198 va_start(params, fmt);
58e4e511 199 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
b875036e
TR
200 va_end(params);
201}
202
4df5e918 203#undef error_errno
fd1d6723
NTND
204int error_errno(const char *fmt, ...)
205{
206 char buf[1024];
207 va_list params;
208
209 va_start(params, fmt);
210 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
211 va_end(params);
212 return -1;
213}
214
e208f9cc 215#undef error
0fcfd160
LT
216int error(const char *err, ...)
217{
218 va_list params;
219
220 va_start(params, err);
39a3f5ea 221 error_routine(err, params);
0fcfd160
LT
222 va_end(params);
223 return -1;
224}
fa39b6b5 225
fd1d6723
NTND
226void warning_errno(const char *warn, ...)
227{
228 char buf[1024];
229 va_list params;
230
231 va_start(params, warn);
232 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
233 va_end(params);
234}
235
46efd2d9 236void warning(const char *warn, ...)
fa39b6b5
SP
237{
238 va_list params;
239
240 va_start(params, warn);
241 warn_routine(warn, params);
242 va_end(params);
243}
d8193743 244
a86303cb
JS
245/* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
246int BUG_exit_code;
247
d8193743
JK
248static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
249{
250 char prefix[256];
251
252 /* truncation via snprintf is OK here */
253 if (file)
254 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
255 else
256 snprintf(prefix, sizeof(prefix), "BUG: ");
257
258 vreportf(prefix, fmt, params);
a86303cb
JS
259 if (BUG_exit_code)
260 exit(BUG_exit_code);
d8193743
JK
261 abort();
262}
263
264#ifdef HAVE_VARIADIC_MACROS
3d7dd2d3 265NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
d8193743
JK
266{
267 va_list ap;
268 va_start(ap, fmt);
269 BUG_vfl(file, line, fmt, ap);
270 va_end(ap);
271}
272#else
3d7dd2d3 273NORETURN void BUG(const char *fmt, ...)
d8193743
JK
274{
275 va_list ap;
276 va_start(ap, fmt);
277 BUG_vfl(NULL, 0, fmt, ap);
278 va_end(ap);
279}
280#endif
0e5bba53
JK
281
282#ifdef SUPPRESS_ANNOTATED_LEAKS
283void unleak_memory(const void *ptr, size_t len)
284{
285 static struct suppressed_leak_root {
286 struct suppressed_leak_root *next;
287 char data[FLEX_ARRAY];
288 } *suppressed_leaks;
289 struct suppressed_leak_root *root;
290
291 FLEX_ALLOC_MEM(root, data, ptr, len);
292 root->next = suppressed_leaks;
293 suppressed_leaks = root;
294}
295#endif