Merge branch 'jc/fix-tree-walk' (early part)
[git/git.git] / usage.c
CommitLineData
0fcfd160
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
4050c0df 6#include "git-compat-util.h"
0fcfd160
LT
7
8static void report(const char *prefix, const char *err, va_list params)
9{
389d1767 10 char msg[1024];
d048a96e
NP
11 vsnprintf(msg, sizeof(msg), err, params);
12 fprintf(stderr, "%s%s\n", prefix, msg);
0fcfd160
LT
13}
14
64b1cb74 15static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 16{
64b1cb74 17 report("usage: ", err, params);
5d1a5c02 18 exit(129);
0fcfd160
LT
19}
20
ce88ac5b 21static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea
PB
22{
23 report("fatal: ", err, params);
24 exit(128);
25}
26
ce88ac5b 27static void error_builtin(const char *err, va_list params)
39a3f5ea
PB
28{
29 report("error: ", err, params);
30}
31
fa39b6b5
SP
32static void warn_builtin(const char *warn, va_list params)
33{
34 report("warning: ", warn, params);
35}
39a3f5ea
PB
36
37/* If we are in a dlopen()ed .so write to a global variable would segfault
38 * (ugh), so keep things static. */
64b1cb74 39static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 40static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 41static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 42static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
39a3f5ea 43
18660bc9 44void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
45{
46 die_routine = routine;
47}
48
64b1cb74
JN
49void usagef(const char *err, ...)
50{
51 va_list params;
52
53 va_start(params, err);
54 usage_routine(err, params);
55 va_end(params);
56}
57
39a3f5ea
PB
58void usage(const char *err)
59{
64b1cb74 60 usagef("%s", err);
39a3f5ea
PB
61}
62
0fcfd160
LT
63void die(const char *err, ...)
64{
65 va_list params;
66
67 va_start(params, err);
39a3f5ea 68 die_routine(err, params);
0fcfd160 69 va_end(params);
0fcfd160
LT
70}
71
b875036e
TR
72void die_errno(const char *fmt, ...)
73{
74 va_list params;
75 char fmt_with_err[1024];
f8b5a8e1
JH
76 char str_error[256], *err;
77 int i, j;
78
79 err = strerror(errno);
80 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
81 if ((str_error[j++] = err[i++]) != '%')
82 continue;
83 if (j < sizeof(str_error) - 1) {
84 str_error[j++] = '%';
85 } else {
86 /* No room to double the '%', so we overwrite it with
87 * '\0' below */
88 j--;
89 break;
90 }
91 }
92 str_error[j] = 0;
93 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
b875036e
TR
94
95 va_start(params, fmt);
96 die_routine(fmt_with_err, params);
97 va_end(params);
98}
99
0fcfd160
LT
100int error(const char *err, ...)
101{
102 va_list params;
103
104 va_start(params, err);
39a3f5ea 105 error_routine(err, params);
0fcfd160
LT
106 va_end(params);
107 return -1;
108}
fa39b6b5 109
46efd2d9 110void warning(const char *warn, ...)
fa39b6b5
SP
111{
112 va_list params;
113
114 va_start(params, warn);
115 warn_routine(warn, params);
116 va_end(params);
117}