]> git.ipfire.org Git - thirdparty/git.git/blame - usage.c
wrapper.h: move declarations for wrapper.c functions from cache.h
[thirdparty/git.git] / usage.c
CommitLineData
0fcfd160
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
3bc4181f 6#include "cache.h"
f394e093 7#include "gettext.h"
d5ebb50d 8#include "wrapper.h"
0fcfd160 9
f5c39c32 10static void vreportf(const char *prefix, const char *err, va_list params)
0fcfd160 11{
b5a9e435 12 char msg[4096];
116d1fa6
JS
13 char *p, *pend = msg + sizeof(msg);
14 size_t prefix_len = strlen(prefix);
f4c3edc0 15
116d1fa6
JS
16 if (sizeof(msg) <= prefix_len) {
17 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
18 abort();
19 }
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 */
24
25 for (; p != pend - 1 && *p; p++) {
f2900898
JK
26 if (iscntrl(*p) && *p != '\t' && *p != '\n')
27 *p = '?';
f4c3edc0 28 }
116d1fa6
JS
29
30 *(p++) = '\n'; /* we no longer need a NUL */
31 fflush(stderr);
32 write_in_full(2, msg, p - msg);
3bc4181f
CB
33}
34
64b1cb74 35static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 36{
a1fd2cf8 37 vreportf(_("usage: "), err, params);
ee4512ed
JH
38
39 /*
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.
43 */
44 trace2_cmd_name("_usage_");
45
46 /*
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().
51 *
52 * TODO It would be nice to update the call sites to pass both
53 * the static usage string and the detailed error message.
54 */
55
5d1a5c02 56 exit(129);
0fcfd160
LT
57}
58
18568ee8
ÆAB
59static void die_message_builtin(const char *err, va_list params)
60{
61 trace2_cmd_error_va(err, params);
a1fd2cf8 62 vreportf(_("fatal: "), err, params);
18568ee8
ÆAB
63}
64
c00c7382
ÆAB
65/*
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).
69 */
ce88ac5b 70static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 71{
18568ee8 72 report_fn die_message_fn = get_die_message_routine();
ee4512ed 73
18568ee8 74 die_message_fn(err, params);
39a3f5ea
PB
75 exit(128);
76}
77
ce88ac5b 78static void error_builtin(const char *err, va_list params)
39a3f5ea 79{
ee4512ed
JH
80 trace2_cmd_error_va(err, params);
81
a1fd2cf8 82 vreportf(_("error: "), err, params);
39a3f5ea
PB
83}
84
fa39b6b5
SP
85static void warn_builtin(const char *warn, va_list params)
86{
0ee10fd1
JT
87 trace2_cmd_error_va(warn, params);
88
a1fd2cf8 89 vreportf(_("warning: "), warn, params);
fa39b6b5 90}
39a3f5ea 91
c19a490e
JK
92static int die_is_recursing_builtin(void)
93{
94 static int dying;
2d3c02f5
ÆAB
95 /*
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.
100 */
101 static const int recursion_limit = 1024;
102
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;
111 }
c19a490e
JK
112}
113
39a3f5ea
PB
114/* If we are in a dlopen()ed .so write to a global variable would segfault
115 * (ugh), so keep things static. */
5710dcce
JK
116static NORETURN_PTR report_fn usage_routine = usage_builtin;
117static NORETURN_PTR report_fn die_routine = die_builtin;
18568ee8 118static report_fn die_message_routine = die_message_builtin;
5710dcce
JK
119static report_fn error_routine = error_builtin;
120static report_fn warn_routine = warn_builtin;
c19a490e 121static int (*die_is_recursing)(void) = die_is_recursing_builtin;
39a3f5ea 122
5710dcce 123void set_die_routine(NORETURN_PTR report_fn routine)
39a3f5ea
PB
124{
125 die_routine = routine;
126}
127
18568ee8
ÆAB
128report_fn get_die_message_routine(void)
129{
130 return die_message_routine;
131}
132
5710dcce 133void set_error_routine(report_fn routine)
3bc4181f
CB
134{
135 error_routine = routine;
136}
137
5710dcce 138report_fn get_error_routine(void)
725149be
CC
139{
140 return error_routine;
141}
142
5710dcce 143void set_warn_routine(report_fn routine)
b83f108b
CC
144{
145 warn_routine = routine;
146}
147
5710dcce 148report_fn get_warn_routine(void)
725149be
CC
149{
150 return warn_routine;
151}
152
c19a490e
JK
153void set_die_is_recursing_routine(int (*routine)(void))
154{
155 die_is_recursing = routine;
156}
157
c2e86add 158void NORETURN usagef(const char *err, ...)
64b1cb74
JN
159{
160 va_list params;
161
162 va_start(params, err);
163 usage_routine(err, params);
164 va_end(params);
165}
166
c2e86add 167void NORETURN usage(const char *err)
39a3f5ea 168{
64b1cb74 169 usagef("%s", err);
39a3f5ea
PB
170}
171
c2e86add 172void NORETURN die(const char *err, ...)
0fcfd160
LT
173{
174 va_list params;
175
c19a490e 176 if (die_is_recursing()) {
cd163d4b
BC
177 fputs("fatal: recursion detected in die handler\n", stderr);
178 exit(128);
179 }
cd163d4b 180
0fcfd160 181 va_start(params, err);
39a3f5ea 182 die_routine(err, params);
0fcfd160 183 va_end(params);
0fcfd160
LT
184}
185
58e4e511 186static const char *fmt_with_err(char *buf, int n, const char *fmt)
b875036e 187{
f8b5a8e1
JH
188 char str_error[256], *err;
189 int i, j;
190
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;
202 }
203 }
204 str_error[j] = 0;
ac4896f0 205 /* Truncation is acceptable here */
58e4e511
NTND
206 snprintf(buf, n, "%s: %s", fmt, str_error);
207 return buf;
208}
209
210void NORETURN die_errno(const char *fmt, ...)
211{
212 char buf[1024];
213 va_list params;
214
215 if (die_is_recursing()) {
216 fputs("fatal: recursion detected in die_errno handler\n",
217 stderr);
218 exit(128);
219 }
b875036e
TR
220
221 va_start(params, fmt);
58e4e511 222 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
b875036e
TR
223 va_end(params);
224}
225
18568ee8
ÆAB
226#undef die_message
227int die_message(const char *err, ...)
228{
229 va_list params;
230
231 va_start(params, err);
232 die_message_routine(err, params);
233 va_end(params);
234 return 128;
235}
236
24f6e6d6
ÆAB
237#undef die_message_errno
238int die_message_errno(const char *fmt, ...)
239{
240 char buf[1024];
241 va_list params;
242
243 va_start(params, fmt);
244 die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
245 va_end(params);
246 return 128;
247}
248
4df5e918 249#undef error_errno
fd1d6723
NTND
250int error_errno(const char *fmt, ...)
251{
252 char buf[1024];
253 va_list params;
254
255 va_start(params, fmt);
256 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
257 va_end(params);
258 return -1;
259}
260
e208f9cc 261#undef error
0fcfd160
LT
262int error(const char *err, ...)
263{
264 va_list params;
265
266 va_start(params, err);
39a3f5ea 267 error_routine(err, params);
0fcfd160
LT
268 va_end(params);
269 return -1;
270}
fa39b6b5 271
fd1d6723
NTND
272void warning_errno(const char *warn, ...)
273{
274 char buf[1024];
275 va_list params;
276
277 va_start(params, warn);
278 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
279 va_end(params);
280}
281
46efd2d9 282void warning(const char *warn, ...)
fa39b6b5
SP
283{
284 va_list params;
285
286 va_start(params, warn);
287 warn_routine(warn, params);
288 va_end(params);
289}
d8193743 290
a86303cb
JS
291/* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
292int BUG_exit_code;
293
0cc05b04
ÆAB
294static void BUG_vfl_common(const char *file, int line, const char *fmt,
295 va_list params)
d8193743
JK
296{
297 char prefix[256];
298
299 /* truncation via snprintf is OK here */
56a29d2c 300 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
d8193743
JK
301
302 vreportf(prefix, fmt, params);
0cc05b04
ÆAB
303}
304
305static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
306{
307 va_list params_copy;
308 static int in_bug;
309
310 va_copy(params_copy, params);
311 BUG_vfl_common(file, line, fmt, params);
0a9dde4a
JT
312
313 if (in_bug)
314 abort();
315 in_bug = 1;
316
317 trace2_cmd_error_va(fmt, params_copy);
318
a86303cb
JS
319 if (BUG_exit_code)
320 exit(BUG_exit_code);
d8193743
JK
321 abort();
322}
323
3d7dd2d3 324NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
d8193743
JK
325{
326 va_list ap;
0cc05b04
ÆAB
327
328 bug_called_must_BUG = 0;
329
d8193743
JK
330 va_start(ap, fmt);
331 BUG_vfl(file, line, fmt, ap);
332 va_end(ap);
333}
0e5bba53 334
0cc05b04
ÆAB
335int bug_called_must_BUG;
336void bug_fl(const char *file, int line, const char *fmt, ...)
337{
f8535596 338 va_list ap;
0cc05b04
ÆAB
339
340 bug_called_must_BUG = 1;
341
0cc05b04
ÆAB
342 va_start(ap, fmt);
343 BUG_vfl_common(file, line, fmt, ap);
344 va_end(ap);
f8535596
JK
345
346 va_start(ap, fmt);
347 trace2_cmd_error_va(fmt, ap);
348 va_end(ap);
0cc05b04
ÆAB
349}
350
0e5bba53
JK
351#ifdef SUPPRESS_ANNOTATED_LEAKS
352void unleak_memory(const void *ptr, size_t len)
353{
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;
359
360 FLEX_ALLOC_MEM(root, data, ptr, len);
361 root->next = suppressed_leaks;
362 suppressed_leaks = root;
363}
364#endif