Add a simple getpass() for MinGW
[4msysgit-hv.git] / usage.c
blob9e35e8a39046992110c235ba79b21bb81d2d69d5
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "git-compat-util.h"
8 void vreport(const char *prefix, const char *err, va_list params)
10 char msg[1024];
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);
18 exit(129);
21 static NORETURN void die_builtin(const char *err, va_list params)
23 vreport("fatal: ", err, params);
24 exit(128);
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();
47 struct routines {
48 die_fn_t die_routine;
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);
59 if (r == NULL) {
60 /* avoid die()! */
61 r = calloc(sizeof(*r), 1);
62 if (r == NULL) {
63 fprintf(stderr, "cannot allocate thread-local storage");
64 return;
66 TlsSetValue(tls_index, r);
68 r->die_routine = routine;
71 void usage(const char *err)
73 usage_routine(err);
76 void die(const char *err, ...)
78 va_list params;
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);
84 else
85 r->die_routine(err, params);
86 va_end(params);
89 int error(const char *err, ...)
91 va_list params;
93 va_start(params, err);
94 error_routine(err, params);
95 va_end(params);
96 return -1;
99 void warning(const char *warn, ...)
101 va_list params;
103 va_start(params, warn);
104 warn_routine(warn, params);
105 va_end(params);