The second batch
[git] / usage.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "git-compat-util.h"
7 #include "cache.h"
8
9 void vreportf(const char *prefix, const char *err, va_list params)
10 {
11         char msg[4096];
12         char *p, *pend = msg + sizeof(msg);
13         size_t prefix_len = strlen(prefix);
14
15         if (sizeof(msg) <= prefix_len) {
16                 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
17                 abort();
18         }
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 */
23
24         for (; p != pend - 1 && *p; p++) {
25                 if (iscntrl(*p) && *p != '\t' && *p != '\n')
26                         *p = '?';
27         }
28
29         *(p++) = '\n'; /* we no longer need a NUL */
30         fflush(stderr);
31         write_in_full(2, msg, p - msg);
32 }
33
34 static NORETURN void usage_builtin(const char *err, va_list params)
35 {
36         vreportf("usage: ", err, params);
37
38         /*
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.
42          */
43         trace2_cmd_name("_usage_");
44
45         /*
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().
50          *
51          * TODO It would be nice to update the call sites to pass both
52          * the static usage string and the detailed error message.
53          */
54
55         exit(129);
56 }
57
58 /*
59  * We call trace2_cmd_error_va() in the below functions first and
60  * expect it to va_copy 'params' before using it (because an 'ap' can
61  * only be walked once).
62  */
63 static NORETURN void die_builtin(const char *err, va_list params)
64 {
65         trace2_cmd_error_va(err, params);
66
67         vreportf("fatal: ", err, params);
68
69         exit(128);
70 }
71
72 static void error_builtin(const char *err, va_list params)
73 {
74         trace2_cmd_error_va(err, params);
75
76         vreportf("error: ", err, params);
77 }
78
79 static void warn_builtin(const char *warn, va_list params)
80 {
81         trace2_cmd_error_va(warn, params);
82
83         vreportf("warning: ", warn, params);
84 }
85
86 static int die_is_recursing_builtin(void)
87 {
88         static int dying;
89         /*
90          * Just an arbitrary number X where "a < x < b" where "a" is
91          * "maximum number of pthreads we'll ever plausibly spawn" and
92          * "b" is "something less than Inf", since the point is to
93          * prevent infinite recursion.
94          */
95         static const int recursion_limit = 1024;
96
97         dying++;
98         if (dying > recursion_limit) {
99                 return 1;
100         } else if (dying == 2) {
101                 warning("die() called many times. Recursion error or racy threaded death!");
102                 return 0;
103         } else {
104                 return 0;
105         }
106 }
107
108 /* If we are in a dlopen()ed .so write to a global variable would segfault
109  * (ugh), so keep things static. */
110 static NORETURN_PTR report_fn usage_routine = usage_builtin;
111 static NORETURN_PTR report_fn die_routine = die_builtin;
112 static report_fn error_routine = error_builtin;
113 static report_fn warn_routine = warn_builtin;
114 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
115
116 void set_die_routine(NORETURN_PTR report_fn routine)
117 {
118         die_routine = routine;
119 }
120
121 void set_error_routine(report_fn routine)
122 {
123         error_routine = routine;
124 }
125
126 report_fn get_error_routine(void)
127 {
128         return error_routine;
129 }
130
131 void set_warn_routine(report_fn routine)
132 {
133         warn_routine = routine;
134 }
135
136 report_fn get_warn_routine(void)
137 {
138         return warn_routine;
139 }
140
141 void set_die_is_recursing_routine(int (*routine)(void))
142 {
143         die_is_recursing = routine;
144 }
145
146 void NORETURN usagef(const char *err, ...)
147 {
148         va_list params;
149
150         va_start(params, err);
151         usage_routine(err, params);
152         va_end(params);
153 }
154
155 void NORETURN usage(const char *err)
156 {
157         usagef("%s", err);
158 }
159
160 void NORETURN die(const char *err, ...)
161 {
162         va_list params;
163
164         if (die_is_recursing()) {
165                 fputs("fatal: recursion detected in die handler\n", stderr);
166                 exit(128);
167         }
168
169         va_start(params, err);
170         die_routine(err, params);
171         va_end(params);
172 }
173
174 static const char *fmt_with_err(char *buf, int n, const char *fmt)
175 {
176         char str_error[256], *err;
177         int i, j;
178
179         err = strerror(errno);
180         for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
181                 if ((str_error[j++] = err[i++]) != '%')
182                         continue;
183                 if (j < sizeof(str_error) - 1) {
184                         str_error[j++] = '%';
185                 } else {
186                         /* No room to double the '%', so we overwrite it with
187                          * '\0' below */
188                         j--;
189                         break;
190                 }
191         }
192         str_error[j] = 0;
193         /* Truncation is acceptable here */
194         snprintf(buf, n, "%s: %s", fmt, str_error);
195         return buf;
196 }
197
198 void NORETURN die_errno(const char *fmt, ...)
199 {
200         char buf[1024];
201         va_list params;
202
203         if (die_is_recursing()) {
204                 fputs("fatal: recursion detected in die_errno handler\n",
205                         stderr);
206                 exit(128);
207         }
208
209         va_start(params, fmt);
210         die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
211         va_end(params);
212 }
213
214 #undef error_errno
215 int error_errno(const char *fmt, ...)
216 {
217         char buf[1024];
218         va_list params;
219
220         va_start(params, fmt);
221         error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
222         va_end(params);
223         return -1;
224 }
225
226 #undef error
227 int error(const char *err, ...)
228 {
229         va_list params;
230
231         va_start(params, err);
232         error_routine(err, params);
233         va_end(params);
234         return -1;
235 }
236
237 void warning_errno(const char *warn, ...)
238 {
239         char buf[1024];
240         va_list params;
241
242         va_start(params, warn);
243         warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
244         va_end(params);
245 }
246
247 void warning(const char *warn, ...)
248 {
249         va_list params;
250
251         va_start(params, warn);
252         warn_routine(warn, params);
253         va_end(params);
254 }
255
256 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
257 int BUG_exit_code;
258
259 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
260 {
261         char prefix[256];
262         va_list params_copy;
263         static int in_bug;
264
265         va_copy(params_copy, params);
266
267         /* truncation via snprintf is OK here */
268         if (file)
269                 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
270         else
271                 snprintf(prefix, sizeof(prefix), "BUG: ");
272
273         vreportf(prefix, fmt, params);
274
275         if (in_bug)
276                 abort();
277         in_bug = 1;
278
279         trace2_cmd_error_va(fmt, params_copy);
280
281         if (BUG_exit_code)
282                 exit(BUG_exit_code);
283         abort();
284 }
285
286 #ifdef HAVE_VARIADIC_MACROS
287 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
288 {
289         va_list ap;
290         va_start(ap, fmt);
291         BUG_vfl(file, line, fmt, ap);
292         va_end(ap);
293 }
294 #else
295 NORETURN void BUG(const char *fmt, ...)
296 {
297         va_list ap;
298         va_start(ap, fmt);
299         BUG_vfl(NULL, 0, fmt, ap);
300         va_end(ap);
301 }
302 #endif
303
304 #ifdef SUPPRESS_ANNOTATED_LEAKS
305 void unleak_memory(const void *ptr, size_t len)
306 {
307         static struct suppressed_leak_root {
308                 struct suppressed_leak_root *next;
309                 char data[FLEX_ARRAY];
310         } *suppressed_leaks;
311         struct suppressed_leak_root *root;
312
313         FLEX_ALLOC_MEM(root, data, ptr, len);
314         root->next = suppressed_leaks;
315         suppressed_leaks = root;
316 }
317 #endif