2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
11 void vreportf(const char *prefix, const char *err, va_list params)
14 vsnprintf(msg, sizeof(msg), err, params);
15 fprintf(stderr, "%s%s\n", prefix, msg);
18 void vwritef(int fd, const char *prefix, const char *err, va_list params)
21 int len = vsnprintf(msg, sizeof(msg), err, params);
22 if (len > sizeof(msg))
25 write_in_full(fd, prefix, strlen(prefix));
26 write_in_full(fd, msg, len);
27 write_in_full(fd, "\n", 1);
30 static NORETURN void usage_builtin(const char *err, va_list params)
32 vreportf("usage: ", err, params);
36 static NORETURN void die_builtin(const char *err, va_list params)
38 vreportf("fatal: ", err, params);
42 static void error_builtin(const char *err, va_list params)
44 vreportf("error: ", err, params);
47 static void warn_builtin(const char *warn, va_list params)
49 vreportf("warning: ", warn, params);
52 /* If we are in a dlopen()ed .so write to a global variable would segfault
53 * (ugh), so keep things static. */
54 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
55 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
56 static void (*error_routine)(const char *err, va_list params) = error_builtin;
57 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
59 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
61 die_routine = routine;
64 void set_error_routine(void (*routine)(const char *err, va_list params))
66 error_routine = routine;
69 void NORETURN usagef(const char *err, ...)
73 va_start(params, err);
74 usage_routine(err, params);
78 void NORETURN usage(const char *err)
83 void NORETURN die(const char *err, ...)
88 fputs("fatal: recursion detected in die handler\n", stderr);
93 va_start(params, err);
94 die_routine(err, params);
98 void NORETURN die_errno(const char *fmt, ...)
101 char fmt_with_err[1024];
102 char str_error[256], *err;
106 fputs("fatal: recursion detected in die_errno handler\n",
112 err = strerror(errno);
113 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
114 if ((str_error[j++] = err[i++]) != '%')
116 if (j < sizeof(str_error) - 1) {
117 str_error[j++] = '%';
119 /* No room to double the '%', so we overwrite it with
126 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
128 va_start(params, fmt);
129 die_routine(fmt_with_err, params);
134 int error(const char *err, ...)
138 va_start(params, err);
139 error_routine(err, params);
144 void warning(const char *warn, ...)
148 va_start(params, warn);
149 warn_routine(warn, params);