Merge branch 'jk/diff-blob' into maint
[git/git-svn.git] / usage.c
blob2f87ca69a8299485ee013187f99ffbdb2618d7c8
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;
14 vsnprintf(msg, sizeof(msg), err, params);
15 for (p = msg; *p; p++) {
16 if (iscntrl(*p) && *p != '\t' && *p != '\n')
17 *p = '?';
19 fprintf(stderr, "%s%s\n", prefix, msg);
22 static NORETURN void usage_builtin(const char *err, va_list params)
24 vreportf("usage: ", err, params);
25 exit(129);
28 static NORETURN void die_builtin(const char *err, va_list params)
30 vreportf("fatal: ", err, params);
31 exit(128);
34 static void error_builtin(const char *err, va_list params)
36 vreportf("error: ", err, params);
39 static void warn_builtin(const char *warn, va_list params)
41 vreportf("warning: ", warn, params);
44 static int die_is_recursing_builtin(void)
46 static int dying;
47 return dying++;
50 /* If we are in a dlopen()ed .so write to a global variable would segfault
51 * (ugh), so keep things static. */
52 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
53 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
54 static void (*error_routine)(const char *err, va_list params) = error_builtin;
55 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
56 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
58 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
60 die_routine = routine;
63 void set_error_routine(void (*routine)(const char *err, va_list params))
65 error_routine = routine;
68 void (*get_error_routine(void))(const char *err, va_list params)
70 return error_routine;
73 void set_warn_routine(void (*routine)(const char *warn, va_list params))
75 warn_routine = routine;
78 void (*get_warn_routine(void))(const char *warn, va_list params)
80 return warn_routine;
83 void set_die_is_recursing_routine(int (*routine)(void))
85 die_is_recursing = routine;
88 void NORETURN usagef(const char *err, ...)
90 va_list params;
92 va_start(params, err);
93 usage_routine(err, params);
94 va_end(params);
97 void NORETURN usage(const char *err)
99 usagef("%s", err);
102 void NORETURN die(const char *err, ...)
104 va_list params;
106 if (die_is_recursing()) {
107 fputs("fatal: recursion detected in die handler\n", stderr);
108 exit(128);
111 va_start(params, err);
112 die_routine(err, params);
113 va_end(params);
116 static const char *fmt_with_err(char *buf, int n, const char *fmt)
118 char str_error[256], *err;
119 int i, j;
121 err = strerror(errno);
122 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
123 if ((str_error[j++] = err[i++]) != '%')
124 continue;
125 if (j < sizeof(str_error) - 1) {
126 str_error[j++] = '%';
127 } else {
128 /* No room to double the '%', so we overwrite it with
129 * '\0' below */
130 j--;
131 break;
134 str_error[j] = 0;
135 snprintf(buf, n, "%s: %s", fmt, str_error);
136 return buf;
139 void NORETURN die_errno(const char *fmt, ...)
141 char buf[1024];
142 va_list params;
144 if (die_is_recursing()) {
145 fputs("fatal: recursion detected in die_errno handler\n",
146 stderr);
147 exit(128);
150 va_start(params, fmt);
151 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
152 va_end(params);
155 #undef error_errno
156 int error_errno(const char *fmt, ...)
158 char buf[1024];
159 va_list params;
161 va_start(params, fmt);
162 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
163 va_end(params);
164 return -1;
167 #undef error
168 int error(const char *err, ...)
170 va_list params;
172 va_start(params, err);
173 error_routine(err, params);
174 va_end(params);
175 return -1;
178 void warning_errno(const char *warn, ...)
180 char buf[1024];
181 va_list params;
183 va_start(params, warn);
184 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
185 va_end(params);
188 void warning(const char *warn, ...)
190 va_list params;
192 va_start(params, warn);
193 warn_routine(warn, params);
194 va_end(params);
197 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
199 char prefix[256];
201 /* truncation via snprintf is OK here */
202 if (file)
203 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
204 else
205 snprintf(prefix, sizeof(prefix), "BUG: ");
207 vreportf(prefix, fmt, params);
208 abort();
211 #ifdef HAVE_VARIADIC_MACROS
212 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
214 va_list ap;
215 va_start(ap, fmt);
216 BUG_vfl(file, line, fmt, ap);
217 va_end(ap);
219 #else
220 NORETURN void BUG(const char *fmt, ...)
222 va_list ap;
223 va_start(ap, fmt);
224 BUG_vfl(NULL, 0, fmt, ap);
225 va_end(ap);
227 #endif