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