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