vreportf: report to arbitrary filehandles
[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{
625a860c 13 char msg[4096];
3b331e92 14 FILE *fh = error_handle ? error_handle : stderr;
d048a96e 15 vsnprintf(msg, sizeof(msg), err, params);
3b331e92 16 fprintf(fh, "%s%s\n", prefix, msg);
3bc4181f
CB
17}
18
64b1cb74 19static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 20{
ebaa79f4 21 vreportf("usage: ", err, params);
5d1a5c02 22 exit(129);
0fcfd160
LT
23}
24
ce88ac5b 25static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 26{
ebaa79f4 27 vreportf("fatal: ", err, params);
39a3f5ea
PB
28 exit(128);
29}
30
ce88ac5b 31static void error_builtin(const char *err, va_list params)
39a3f5ea 32{
ebaa79f4 33 vreportf("error: ", err, params);
39a3f5ea
PB
34}
35
fa39b6b5
SP
36static void warn_builtin(const char *warn, va_list params)
37{
ebaa79f4 38 vreportf("warning: ", warn, params);
fa39b6b5 39}
39a3f5ea 40
c19a490e
JK
41static int die_is_recursing_builtin(void)
42{
43 static int dying;
44 return dying++;
45}
46
39a3f5ea
PB
47/* If we are in a dlopen()ed .so write to a global variable would segfault
48 * (ugh), so keep things static. */
64b1cb74 49static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 50static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 51static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 52static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
c19a490e 53static int (*die_is_recursing)(void) = die_is_recursing_builtin;
39a3f5ea 54
18660bc9 55void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
56{
57 die_routine = routine;
58}
59
3bc4181f
CB
60void set_error_routine(void (*routine)(const char *err, va_list params))
61{
62 error_routine = routine;
63}
64
c19a490e
JK
65void set_die_is_recursing_routine(int (*routine)(void))
66{
67 die_is_recursing = routine;
68}
69
3b331e92
JK
70void set_error_handle(FILE *fh)
71{
72 error_handle = fh;
73}
74
c2e86add 75void NORETURN usagef(const char *err, ...)
64b1cb74
JN
76{
77 va_list params;
78
79 va_start(params, err);
80 usage_routine(err, params);
81 va_end(params);
82}
83
c2e86add 84void NORETURN usage(const char *err)
39a3f5ea 85{
64b1cb74 86 usagef("%s", err);
39a3f5ea
PB
87}
88
c2e86add 89void NORETURN die(const char *err, ...)
0fcfd160
LT
90{
91 va_list params;
92
c19a490e 93 if (die_is_recursing()) {
cd163d4b
BC
94 fputs("fatal: recursion detected in die handler\n", stderr);
95 exit(128);
96 }
cd163d4b 97
0fcfd160 98 va_start(params, err);
39a3f5ea 99 die_routine(err, params);
0fcfd160 100 va_end(params);
0fcfd160
LT
101}
102
c2e86add 103void NORETURN die_errno(const char *fmt, ...)
b875036e
TR
104{
105 va_list params;
106 char fmt_with_err[1024];
f8b5a8e1
JH
107 char str_error[256], *err;
108 int i, j;
109
c19a490e 110 if (die_is_recursing()) {
cd163d4b
BC
111 fputs("fatal: recursion detected in die_errno handler\n",
112 stderr);
113 exit(128);
114 }
cd163d4b 115
f8b5a8e1
JH
116 err = strerror(errno);
117 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
118 if ((str_error[j++] = err[i++]) != '%')
119 continue;
120 if (j < sizeof(str_error) - 1) {
121 str_error[j++] = '%';
122 } else {
123 /* No room to double the '%', so we overwrite it with
124 * '\0' below */
125 j--;
126 break;
127 }
128 }
129 str_error[j] = 0;
130 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
b875036e
TR
131
132 va_start(params, fmt);
133 die_routine(fmt_with_err, params);
134 va_end(params);
135}
136
e208f9cc 137#undef error
0fcfd160
LT
138int error(const char *err, ...)
139{
140 va_list params;
141
142 va_start(params, err);
39a3f5ea 143 error_routine(err, params);
0fcfd160
LT
144 va_end(params);
145 return -1;
146}
fa39b6b5 147
46efd2d9 148void warning(const char *warn, ...)
fa39b6b5
SP
149{
150 va_list params;
151
152 va_start(params, warn);
153 warn_routine(warn, params);
154 va_end(params);
155}