Git 2.13
[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
3b331e92
JK
9static FILE *error_handle;
10
ebaa79f4 11void vreportf(const char *prefix, const char *err, va_list params)
0fcfd160 12{
b5a9e435 13 char msg[4096];
3b331e92 14 FILE *fh = error_handle ? error_handle : stderr;
f2900898 15 char *p;
f4c3edc0 16
b5a9e435 17 vsnprintf(msg, sizeof(msg), err, params);
f2900898
JK
18 for (p = msg; *p; p++) {
19 if (iscntrl(*p) && *p != '\t' && *p != '\n')
20 *p = '?';
f4c3edc0 21 }
b5a9e435 22 fprintf(fh, "%s%s\n", prefix, msg);
3bc4181f
CB
23}
24
64b1cb74 25static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 26{
ebaa79f4 27 vreportf("usage: ", err, params);
5d1a5c02 28 exit(129);
0fcfd160
LT
29}
30
ce88ac5b 31static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 32{
ebaa79f4 33 vreportf("fatal: ", err, params);
39a3f5ea
PB
34 exit(128);
35}
36
ce88ac5b 37static void error_builtin(const char *err, va_list params)
39a3f5ea 38{
ebaa79f4 39 vreportf("error: ", err, params);
39a3f5ea
PB
40}
41
fa39b6b5
SP
42static void warn_builtin(const char *warn, va_list params)
43{
ebaa79f4 44 vreportf("warning: ", warn, params);
fa39b6b5 45}
39a3f5ea 46
c19a490e
JK
47static int die_is_recursing_builtin(void)
48{
49 static int dying;
50 return dying++;
51}
52
39a3f5ea
PB
53/* If we are in a dlopen()ed .so write to a global variable would segfault
54 * (ugh), so keep things static. */
64b1cb74 55static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 56static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 57static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 58static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
c19a490e 59static int (*die_is_recursing)(void) = die_is_recursing_builtin;
39a3f5ea 60
18660bc9 61void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
62{
63 die_routine = routine;
64}
65
3bc4181f
CB
66void set_error_routine(void (*routine)(const char *err, va_list params))
67{
68 error_routine = routine;
69}
70
725149be
CC
71void (*get_error_routine(void))(const char *err, va_list params)
72{
73 return error_routine;
74}
75
b83f108b
CC
76void set_warn_routine(void (*routine)(const char *warn, va_list params))
77{
78 warn_routine = routine;
79}
80
725149be
CC
81void (*get_warn_routine(void))(const char *warn, va_list params)
82{
83 return warn_routine;
84}
85
c19a490e
JK
86void set_die_is_recursing_routine(int (*routine)(void))
87{
88 die_is_recursing = routine;
89}
90
3b331e92
JK
91void set_error_handle(FILE *fh)
92{
93 error_handle = fh;
94}
95
c2e86add 96void NORETURN usagef(const char *err, ...)
64b1cb74
JN
97{
98 va_list params;
99
100 va_start(params, err);
101 usage_routine(err, params);
102 va_end(params);
103}
104
c2e86add 105void NORETURN usage(const char *err)
39a3f5ea 106{
64b1cb74 107 usagef("%s", err);
39a3f5ea
PB
108}
109
c2e86add 110void NORETURN die(const char *err, ...)
0fcfd160
LT
111{
112 va_list params;
113
c19a490e 114 if (die_is_recursing()) {
cd163d4b
BC
115 fputs("fatal: recursion detected in die handler\n", stderr);
116 exit(128);
117 }
cd163d4b 118
0fcfd160 119 va_start(params, err);
39a3f5ea 120 die_routine(err, params);
0fcfd160 121 va_end(params);
0fcfd160
LT
122}
123
58e4e511 124static const char *fmt_with_err(char *buf, int n, const char *fmt)
b875036e 125{
f8b5a8e1
JH
126 char str_error[256], *err;
127 int i, j;
128
129 err = strerror(errno);
130 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
131 if ((str_error[j++] = err[i++]) != '%')
132 continue;
133 if (j < sizeof(str_error) - 1) {
134 str_error[j++] = '%';
135 } else {
136 /* No room to double the '%', so we overwrite it with
137 * '\0' below */
138 j--;
139 break;
140 }
141 }
142 str_error[j] = 0;
58e4e511
NTND
143 snprintf(buf, n, "%s: %s", fmt, str_error);
144 return buf;
145}
146
147void NORETURN die_errno(const char *fmt, ...)
148{
149 char buf[1024];
150 va_list params;
151
152 if (die_is_recursing()) {
153 fputs("fatal: recursion detected in die_errno handler\n",
154 stderr);
155 exit(128);
156 }
b875036e
TR
157
158 va_start(params, fmt);
58e4e511 159 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
b875036e
TR
160 va_end(params);
161}
162
4df5e918 163#undef error_errno
fd1d6723
NTND
164int error_errno(const char *fmt, ...)
165{
166 char buf[1024];
167 va_list params;
168
169 va_start(params, fmt);
170 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
171 va_end(params);
172 return -1;
173}
174
e208f9cc 175#undef error
0fcfd160
LT
176int error(const char *err, ...)
177{
178 va_list params;
179
180 va_start(params, err);
39a3f5ea 181 error_routine(err, params);
0fcfd160
LT
182 va_end(params);
183 return -1;
184}
fa39b6b5 185
fd1d6723
NTND
186void warning_errno(const char *warn, ...)
187{
188 char buf[1024];
189 va_list params;
190
191 va_start(params, warn);
192 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
193 va_end(params);
194}
195
46efd2d9 196void warning(const char *warn, ...)
fa39b6b5
SP
197{
198 va_list params;
199
200 va_start(params, warn);
201 warn_routine(warn, params);
202 va_end(params);
203}