set_git_dir: handle feeding gitdir to itself
[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);
5d1a5c02 25 exit(129);
0fcfd160
LT
26}
27
ce88ac5b 28static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 29{
ebaa79f4 30 vreportf("fatal: ", err, params);
39a3f5ea
PB
31 exit(128);
32}
33
ce88ac5b 34static void error_builtin(const char *err, va_list params)
39a3f5ea 35{
ebaa79f4 36 vreportf("error: ", err, params);
39a3f5ea
PB
37}
38
fa39b6b5
SP
39static void warn_builtin(const char *warn, va_list params)
40{
ebaa79f4 41 vreportf("warning: ", warn, params);
fa39b6b5 42}
39a3f5ea 43
c19a490e
JK
44static int die_is_recursing_builtin(void)
45{
46 static int dying;
2d3c02f5
ÆAB
47 /*
48 * Just an arbitrary number X where "a < x < b" where "a" is
49 * "maximum number of pthreads we'll ever plausibly spawn" and
50 * "b" is "something less than Inf", since the point is to
51 * prevent infinite recursion.
52 */
53 static const int recursion_limit = 1024;
54
55 dying++;
56 if (dying > recursion_limit) {
57 return 1;
58 } else if (dying == 2) {
59 warning("die() called many times. Recursion error or racy threaded death!");
60 return 0;
61 } else {
62 return 0;
63 }
c19a490e
JK
64}
65
39a3f5ea
PB
66/* If we are in a dlopen()ed .so write to a global variable would segfault
67 * (ugh), so keep things static. */
64b1cb74 68static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 69static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 70static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 71static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
c19a490e 72static int (*die_is_recursing)(void) = die_is_recursing_builtin;
39a3f5ea 73
18660bc9 74void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
75{
76 die_routine = routine;
77}
78
3bc4181f
CB
79void set_error_routine(void (*routine)(const char *err, va_list params))
80{
81 error_routine = routine;
82}
83
725149be
CC
84void (*get_error_routine(void))(const char *err, va_list params)
85{
86 return error_routine;
87}
88
b83f108b
CC
89void set_warn_routine(void (*routine)(const char *warn, va_list params))
90{
91 warn_routine = routine;
92}
93
725149be
CC
94void (*get_warn_routine(void))(const char *warn, va_list params)
95{
96 return warn_routine;
97}
98
c19a490e
JK
99void set_die_is_recursing_routine(int (*routine)(void))
100{
101 die_is_recursing = routine;
102}
103
c2e86add 104void NORETURN usagef(const char *err, ...)
64b1cb74
JN
105{
106 va_list params;
107
108 va_start(params, err);
109 usage_routine(err, params);
110 va_end(params);
111}
112
c2e86add 113void NORETURN usage(const char *err)
39a3f5ea 114{
64b1cb74 115 usagef("%s", err);
39a3f5ea
PB
116}
117
c2e86add 118void NORETURN die(const char *err, ...)
0fcfd160
LT
119{
120 va_list params;
121
c19a490e 122 if (die_is_recursing()) {
cd163d4b
BC
123 fputs("fatal: recursion detected in die handler\n", stderr);
124 exit(128);
125 }
cd163d4b 126
0fcfd160 127 va_start(params, err);
39a3f5ea 128 die_routine(err, params);
0fcfd160 129 va_end(params);
0fcfd160
LT
130}
131
58e4e511 132static const char *fmt_with_err(char *buf, int n, const char *fmt)
b875036e 133{
f8b5a8e1
JH
134 char str_error[256], *err;
135 int i, j;
136
137 err = strerror(errno);
138 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
139 if ((str_error[j++] = err[i++]) != '%')
140 continue;
141 if (j < sizeof(str_error) - 1) {
142 str_error[j++] = '%';
143 } else {
144 /* No room to double the '%', so we overwrite it with
145 * '\0' below */
146 j--;
147 break;
148 }
149 }
150 str_error[j] = 0;
58e4e511
NTND
151 snprintf(buf, n, "%s: %s", fmt, str_error);
152 return buf;
153}
154
155void NORETURN die_errno(const char *fmt, ...)
156{
157 char buf[1024];
158 va_list params;
159
160 if (die_is_recursing()) {
161 fputs("fatal: recursion detected in die_errno handler\n",
162 stderr);
163 exit(128);
164 }
b875036e
TR
165
166 va_start(params, fmt);
58e4e511 167 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
b875036e
TR
168 va_end(params);
169}
170
4df5e918 171#undef error_errno
fd1d6723
NTND
172int error_errno(const char *fmt, ...)
173{
174 char buf[1024];
175 va_list params;
176
177 va_start(params, fmt);
178 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
179 va_end(params);
180 return -1;
181}
182
e208f9cc 183#undef error
0fcfd160
LT
184int error(const char *err, ...)
185{
186 va_list params;
187
188 va_start(params, err);
39a3f5ea 189 error_routine(err, params);
0fcfd160
LT
190 va_end(params);
191 return -1;
192}
fa39b6b5 193
fd1d6723
NTND
194void warning_errno(const char *warn, ...)
195{
196 char buf[1024];
197 va_list params;
198
199 va_start(params, warn);
200 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
201 va_end(params);
202}
203
46efd2d9 204void warning(const char *warn, ...)
fa39b6b5
SP
205{
206 va_list params;
207
208 va_start(params, warn);
209 warn_routine(warn, params);
210 va_end(params);
211}
d8193743
JK
212
213static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
214{
215 char prefix[256];
216
217 /* truncation via snprintf is OK here */
218 if (file)
219 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
220 else
221 snprintf(prefix, sizeof(prefix), "BUG: ");
222
223 vreportf(prefix, fmt, params);
224 abort();
225}
226
227#ifdef HAVE_VARIADIC_MACROS
3d7dd2d3 228NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
d8193743
JK
229{
230 va_list ap;
231 va_start(ap, fmt);
232 BUG_vfl(file, line, fmt, ap);
233 va_end(ap);
234}
235#else
3d7dd2d3 236NORETURN void BUG(const char *fmt, ...)
d8193743
JK
237{
238 va_list ap;
239 va_start(ap, fmt);
240 BUG_vfl(NULL, 0, fmt, ap);
241 va_end(ap);
242}
243#endif