2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
8 void vreport(const char *prefix
, const char *err
, va_list params
)
11 vsnprintf(msg
, sizeof(msg
), err
, params
);
12 fprintf(stderr
, "%s%s\n", prefix
, msg
);
15 static NORETURN
void usage_builtin(const char *err
)
17 fprintf(stderr
, "usage: %s\n", err
);
21 static NORETURN
void die_builtin(const char *err
, va_list params
)
23 vreport("fatal: ", err
, params
);
27 static void error_builtin(const char *err
, va_list params
)
29 vreport("error: ", err
, params
);
32 static void warn_builtin(const char *warn
, va_list params
)
34 vreport("warning: ", warn
, params
);
37 typedef void (*die_fn_t
)(const char *err
, va_list params
) NORETURN
;
39 static DWORD tls_index
;
41 static void tls_init(void) __attribute__((constructor
));
42 static void tls_init(void)
44 tls_index
= TlsAlloc();
50 /* If we are in a dlopen()ed .so write to a global variable would segfault
51 * (ugh), so keep things static. */
52 static void (*usage_routine
)(const char *err
) NORETURN
= usage_builtin
;
53 static void (*error_routine
)(const char *err
, va_list params
) = error_builtin
;
54 static void (*warn_routine
)(const char *err
, va_list params
) = warn_builtin
;
56 void set_die_routine(void (*routine
)(const char *err
, va_list params
) NORETURN
)
58 struct routines
*r
= TlsGetValue(tls_index
);
61 r
= calloc(sizeof(*r
), 1);
63 fprintf(stderr
, "cannot allocate thread-local storage");
66 TlsSetValue(tls_index
, r
);
68 r
->die_routine
= routine
;
71 void usage(const char *err
)
76 void die(const char *err
, ...)
79 struct routines
*r
= TlsGetValue(tls_index
);
81 va_start(params
, err
);
82 if (r
== NULL
|| r
->die_routine
== NULL
)
83 die_builtin(err
, params
);
85 r
->die_routine(err
, params
);
89 int error(const char *err
, ...)
93 va_start(params
, err
);
94 error_routine(err
, params
);
99 void warning(const char *warn
, ...)
103 va_start(params
, warn
);
104 warn_routine(warn
, params
);