Interested in helping open source friends on HP-UX?
[git/mjg.git] / usage.c
blobed146453cabeb9e82bef0c5610cda47d1b2a0b1c
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 void vreportf(const char *prefix, const char *err, va_list params)
11 char msg[4096];
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)
18 char msg[4096];
19 int len = vsnprintf(msg, sizeof(msg), err, params);
20 if (len > sizeof(msg))
21 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);
31 exit(129);
34 static NORETURN void die_builtin(const char *err, va_list params)
36 vreportf("fatal: ", err, params);
37 exit(128);
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)
52 static int dying;
53 return dying++;
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, ...)
81 va_list params;
83 va_start(params, err);
84 usage_routine(err, params);
85 va_end(params);
88 void NORETURN usage(const char *err)
90 usagef("%s", err);
93 void NORETURN die(const char *err, ...)
95 va_list params;
97 if (die_is_recursing()) {
98 fputs("fatal: recursion detected in die handler\n", stderr);
99 exit(128);
102 va_start(params, err);
103 die_routine(err, params);
104 va_end(params);
107 void NORETURN die_errno(const char *fmt, ...)
109 va_list params;
110 char fmt_with_err[1024];
111 char str_error[256], *err;
112 int i, j;
114 if (die_is_recursing()) {
115 fputs("fatal: recursion detected in die_errno handler\n",
116 stderr);
117 exit(128);
120 err = strerror(errno);
121 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
122 if ((str_error[j++] = err[i++]) != '%')
123 continue;
124 if (j < sizeof(str_error) - 1) {
125 str_error[j++] = '%';
126 } else {
127 /* No room to double the '%', so we overwrite it with
128 * '\0' below */
129 j--;
130 break;
133 str_error[j] = 0;
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);
138 va_end(params);
141 #undef error
142 int error(const char *err, ...)
144 va_list params;
146 va_start(params, err);
147 error_routine(err, params);
148 va_end(params);
149 return -1;
152 void warning(const char *warn, ...)
154 va_list params;
156 va_start(params, warn);
157 warn_routine(warn, params);
158 va_end(params);