refs: move submodule code out of files-backend.c
[git/debian.git] / usage.c
blobad6d2910fb58e2f27a52646bd79a0442e689517f
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;
11 void vreportf(const char *prefix, const char *err, va_list params)
13 char msg[4096];
14 FILE *fh = error_handle ? error_handle : stderr;
15 char *p;
17 vsnprintf(msg, sizeof(msg), err, params);
18 for (p = msg; *p; p++) {
19 if (iscntrl(*p) && *p != '\t' && *p != '\n')
20 *p = '?';
22 fprintf(fh, "%s%s\n", prefix, msg);
25 static NORETURN void usage_builtin(const char *err, va_list params)
27 vreportf("usage: ", err, params);
28 exit(129);
31 static NORETURN void die_builtin(const char *err, va_list params)
33 vreportf("fatal: ", err, params);
34 exit(128);
37 static void error_builtin(const char *err, va_list params)
39 vreportf("error: ", err, params);
42 static void warn_builtin(const char *warn, va_list params)
44 vreportf("warning: ", warn, params);
47 static int die_is_recursing_builtin(void)
49 static int dying;
50 return dying++;
53 /* If we are in a dlopen()ed .so write to a global variable would segfault
54 * (ugh), so keep things static. */
55 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
56 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
57 static void (*error_routine)(const char *err, va_list params) = error_builtin;
58 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
59 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
61 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
63 die_routine = routine;
66 void set_error_routine(void (*routine)(const char *err, va_list params))
68 error_routine = routine;
71 void (*get_error_routine(void))(const char *err, va_list params)
73 return error_routine;
76 void set_warn_routine(void (*routine)(const char *warn, va_list params))
78 warn_routine = routine;
81 void (*get_warn_routine(void))(const char *warn, va_list params)
83 return warn_routine;
86 void set_die_is_recursing_routine(int (*routine)(void))
88 die_is_recursing = routine;
91 void set_error_handle(FILE *fh)
93 error_handle = fh;
96 void NORETURN usagef(const char *err, ...)
98 va_list params;
100 va_start(params, err);
101 usage_routine(err, params);
102 va_end(params);
105 void NORETURN usage(const char *err)
107 usagef("%s", err);
110 void NORETURN die(const char *err, ...)
112 va_list params;
114 if (die_is_recursing()) {
115 fputs("fatal: recursion detected in die handler\n", stderr);
116 exit(128);
119 va_start(params, err);
120 die_routine(err, params);
121 va_end(params);
124 static const char *fmt_with_err(char *buf, int n, const char *fmt)
126 char str_error[256], *err;
127 int i, j;
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;
142 str_error[j] = 0;
143 snprintf(buf, n, "%s: %s", fmt, str_error);
144 return buf;
147 void NORETURN die_errno(const char *fmt, ...)
149 char buf[1024];
150 va_list params;
152 if (die_is_recursing()) {
153 fputs("fatal: recursion detected in die_errno handler\n",
154 stderr);
155 exit(128);
158 va_start(params, fmt);
159 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
160 va_end(params);
163 #undef error_errno
164 int error_errno(const char *fmt, ...)
166 char buf[1024];
167 va_list params;
169 va_start(params, fmt);
170 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
171 va_end(params);
172 return -1;
175 #undef error
176 int error(const char *err, ...)
178 va_list params;
180 va_start(params, err);
181 error_routine(err, params);
182 va_end(params);
183 return -1;
186 void warning_errno(const char *warn, ...)
188 char buf[1024];
189 va_list params;
191 va_start(params, warn);
192 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
193 va_end(params);
196 void warning(const char *warn, ...)
198 va_list params;
200 va_start(params, warn);
201 warn_routine(warn, params);
202 va_end(params);