fast-export: fix argument name in error messages
[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"
3bc4181f 7#include "cache.h"
0fcfd160 8
cd163d4b
BC
9static int dying;
10
ebaa79f4 11void vreportf(const char *prefix, const char *err, va_list params)
0fcfd160 12{
625a860c 13 char msg[4096];
d048a96e
NP
14 vsnprintf(msg, sizeof(msg), err, params);
15 fprintf(stderr, "%s%s\n", prefix, msg);
0fcfd160
LT
16}
17
3bc4181f
CB
18void vwritef(int fd, const char *prefix, const char *err, va_list params)
19{
20 char msg[4096];
21 int len = vsnprintf(msg, sizeof(msg), err, params);
22 if (len > sizeof(msg))
23 len = sizeof(msg);
24
25 write_in_full(fd, prefix, strlen(prefix));
26 write_in_full(fd, msg, len);
27 write_in_full(fd, "\n", 1);
28}
29
64b1cb74 30static NORETURN void usage_builtin(const char *err, va_list params)
0fcfd160 31{
ebaa79f4 32 vreportf("usage: ", err, params);
5d1a5c02 33 exit(129);
0fcfd160
LT
34}
35
ce88ac5b 36static NORETURN void die_builtin(const char *err, va_list params)
39a3f5ea 37{
ebaa79f4 38 vreportf("fatal: ", err, params);
39a3f5ea
PB
39 exit(128);
40}
41
ce88ac5b 42static void error_builtin(const char *err, va_list params)
39a3f5ea 43{
ebaa79f4 44 vreportf("error: ", err, params);
39a3f5ea
PB
45}
46
fa39b6b5
SP
47static void warn_builtin(const char *warn, va_list params)
48{
ebaa79f4 49 vreportf("warning: ", warn, params);
fa39b6b5 50}
39a3f5ea
PB
51
52/* If we are in a dlopen()ed .so write to a global variable would segfault
53 * (ugh), so keep things static. */
64b1cb74 54static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
18660bc9 55static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
39a3f5ea 56static void (*error_routine)(const char *err, va_list params) = error_builtin;
fa39b6b5 57static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
39a3f5ea 58
18660bc9 59void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
39a3f5ea
PB
60{
61 die_routine = routine;
62}
63
3bc4181f
CB
64void set_error_routine(void (*routine)(const char *err, va_list params))
65{
66 error_routine = routine;
67}
68
c2e86add 69void NORETURN usagef(const char *err, ...)
64b1cb74
JN
70{
71 va_list params;
72
73 va_start(params, err);
74 usage_routine(err, params);
75 va_end(params);
76}
77
c2e86add 78void NORETURN usage(const char *err)
39a3f5ea 79{
64b1cb74 80 usagef("%s", err);
39a3f5ea
PB
81}
82
c2e86add 83void NORETURN die(const char *err, ...)
0fcfd160
LT
84{
85 va_list params;
86
cd163d4b
BC
87 if (dying) {
88 fputs("fatal: recursion detected in die handler\n", stderr);
89 exit(128);
90 }
91 dying = 1;
92
0fcfd160 93 va_start(params, err);
39a3f5ea 94 die_routine(err, params);
0fcfd160 95 va_end(params);
0fcfd160
LT
96}
97
c2e86add 98void NORETURN die_errno(const char *fmt, ...)
b875036e
TR
99{
100 va_list params;
101 char fmt_with_err[1024];
f8b5a8e1
JH
102 char str_error[256], *err;
103 int i, j;
104
cd163d4b
BC
105 if (dying) {
106 fputs("fatal: recursion detected in die_errno handler\n",
107 stderr);
108 exit(128);
109 }
110 dying = 1;
111
f8b5a8e1
JH
112 err = strerror(errno);
113 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
114 if ((str_error[j++] = err[i++]) != '%')
115 continue;
116 if (j < sizeof(str_error) - 1) {
117 str_error[j++] = '%';
118 } else {
119 /* No room to double the '%', so we overwrite it with
120 * '\0' below */
121 j--;
122 break;
123 }
124 }
125 str_error[j] = 0;
126 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
b875036e
TR
127
128 va_start(params, fmt);
129 die_routine(fmt_with_err, params);
130 va_end(params);
131}
132
0fcfd160
LT
133int error(const char *err, ...)
134{
135 va_list params;
136
137 va_start(params, err);
39a3f5ea 138 error_routine(err, params);
0fcfd160
LT
139 va_end(params);
140 return -1;
141}
fa39b6b5 142
46efd2d9 143void warning(const char *warn, ...)
fa39b6b5
SP
144{
145 va_list params;
146
147 va_start(params, warn);
148 warn_routine(warn, params);
149 va_end(params);
150}