mingw: fix regression in t1308-config-set
[git/git-svn.git] / usage.c
blob1dad03fb5c9b7f8f7785915d004aff43e39b0d2a
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 static FILE *error_handle;
10 static int tweaked_error_buffering;
12 void vreportf(const char *prefix, const char *err, va_list params)
14 FILE *fh = error_handle ? error_handle : stderr;
16 fflush(fh);
17 if (!tweaked_error_buffering) {
18 setvbuf(fh, NULL, _IOLBF, 0);
19 tweaked_error_buffering = 1;
22 fputs(prefix, fh);
23 vfprintf(fh, err, params);
24 fputc('\n', fh);
27 static NORETURN void usage_builtin(const char *err, va_list params)
29 vreportf("usage: ", err, params);
30 exit(129);
33 static NORETURN void die_builtin(const char *err, va_list params)
35 vreportf("fatal: ", err, params);
36 exit(128);
39 static void error_builtin(const char *err, va_list params)
41 vreportf("error: ", err, params);
44 static void warn_builtin(const char *warn, va_list params)
46 vreportf("warning: ", warn, params);
49 static int die_is_recursing_builtin(void)
51 static int dying;
52 return dying++;
55 /* If we are in a dlopen()ed .so write to a global variable would segfault
56 * (ugh), so keep things static. */
57 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
58 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
59 static void (*error_routine)(const char *err, va_list params) = error_builtin;
60 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
61 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
63 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
65 die_routine = routine;
68 void set_error_routine(void (*routine)(const char *err, va_list params))
70 error_routine = routine;
73 void set_die_is_recursing_routine(int (*routine)(void))
75 die_is_recursing = routine;
78 void set_error_handle(FILE *fh)
80 error_handle = fh;
81 tweaked_error_buffering = 0;
84 void NORETURN usagef(const char *err, ...)
86 va_list params;
88 va_start(params, err);
89 usage_routine(err, params);
90 va_end(params);
93 void NORETURN usage(const char *err)
95 usagef("%s", err);
98 void NORETURN die(const char *err, ...)
100 va_list params;
102 if (die_is_recursing()) {
103 fputs("fatal: recursion detected in die handler\n", stderr);
104 exit(128);
107 va_start(params, err);
108 die_routine(err, params);
109 va_end(params);
112 static const char *fmt_with_err(char *buf, int n, const char *fmt)
114 char str_error[256], *err;
115 int i, j;
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;
130 str_error[j] = 0;
131 snprintf(buf, n, "%s: %s", fmt, str_error);
132 return buf;
135 void NORETURN die_errno(const char *fmt, ...)
137 char buf[1024];
138 va_list params;
140 if (die_is_recursing()) {
141 fputs("fatal: recursion detected in die_errno handler\n",
142 stderr);
143 exit(128);
146 va_start(params, fmt);
147 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
148 va_end(params);
151 int error_errno(const char *fmt, ...)
153 char buf[1024];
154 va_list params;
156 va_start(params, fmt);
157 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
158 va_end(params);
159 return -1;
162 #undef error
163 int error(const char *err, ...)
165 va_list params;
167 va_start(params, err);
168 error_routine(err, params);
169 va_end(params);
170 return -1;
173 void warning_errno(const char *warn, ...)
175 char buf[1024];
176 va_list params;
178 va_start(params, warn);
179 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
180 va_end(params);
183 void warning(const char *warn, ...)
185 va_list params;
187 va_start(params, warn);
188 warn_routine(warn, params);
189 va_end(params);