2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 void vreportf(const char *prefix
, const char *err
, va_list params
)
12 vsnprintf(msg
, sizeof(msg
), err
, params
);
13 fprintf(stderr
, "%s%s\n", prefix
, msg
);
16 void vwritef(int fd
, const char *prefix
, const char *err
, va_list params
)
19 int len
= vsnprintf(msg
, sizeof(msg
), err
, params
);
20 if (len
> sizeof(msg
))
23 write_in_full(fd
, prefix
, strlen(prefix
));
24 write_in_full(fd
, msg
, len
);
25 write_in_full(fd
, "\n", 1);
28 static NORETURN
void usage_builtin(const char *err
, va_list params
)
30 vreportf("usage: ", err
, params
);
34 static NORETURN
void die_builtin(const char *err
, va_list params
)
36 vreportf("fatal: ", err
, params
);
40 static void error_builtin(const char *err
, va_list params
)
42 vreportf("error: ", err
, params
);
45 static void warn_builtin(const char *warn
, va_list params
)
47 vreportf("warning: ", warn
, params
);
50 static int die_is_recursing_builtin(void)
56 /* If we are in a dlopen()ed .so write to a global variable would segfault
57 * (ugh), so keep things static. */
58 static NORETURN_PTR
void (*usage_routine
)(const char *err
, va_list params
) = usage_builtin
;
59 static NORETURN_PTR
void (*die_routine
)(const char *err
, va_list params
) = die_builtin
;
60 static void (*error_routine
)(const char *err
, va_list params
) = error_builtin
;
61 static void (*warn_routine
)(const char *err
, va_list params
) = warn_builtin
;
62 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
64 void set_die_routine(NORETURN_PTR
void (*routine
)(const char *err
, va_list params
))
66 die_routine
= routine
;
69 void set_error_routine(void (*routine
)(const char *err
, va_list params
))
71 error_routine
= routine
;
74 void set_die_is_recursing_routine(int (*routine
)(void))
76 die_is_recursing
= routine
;
79 void NORETURN
usagef(const char *err
, ...)
83 va_start(params
, err
);
84 usage_routine(err
, params
);
88 void NORETURN
usage(const char *err
)
93 void NORETURN
die(const char *err
, ...)
97 if (die_is_recursing()) {
98 fputs("fatal: recursion detected in die handler\n", stderr
);
102 va_start(params
, err
);
103 die_routine(err
, params
);
107 void NORETURN
die_errno(const char *fmt
, ...)
110 char fmt_with_err
[1024];
111 char str_error
[256], *err
;
114 if (die_is_recursing()) {
115 fputs("fatal: recursion detected in die_errno handler\n",
120 err
= strerror(errno
);
121 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
122 if ((str_error
[j
++] = err
[i
++]) != '%')
124 if (j
< sizeof(str_error
) - 1) {
125 str_error
[j
++] = '%';
127 /* No room to double the '%', so we overwrite it with
134 snprintf(fmt_with_err
, sizeof(fmt_with_err
), "%s: %s", fmt
, str_error
);
136 va_start(params
, fmt
);
137 die_routine(fmt_with_err
, params
);
142 int error(const char *err
, ...)
146 va_start(params
, err
);
147 error_routine(err
, params
);
152 void warning(const char *warn
, ...)
156 va_start(params
, warn
);
157 warn_routine(warn
, params
);