French translation: copy -> copie.
[git/dscho.git] / usage.c
bloba2a667800474e315a362e1d0623c17dafaad1022
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "git-compat-util.h"
7 #include "cache.h"
9 void vreportf(const char *prefix, const char *err, va_list params)
11 char msg[4096];
12 vsnprintf(msg, sizeof(msg), err, params);
13 fprintf(stderr, "%s%s\n", prefix, msg);
16 void vwritef(int fd, const char *prefix, const char *err, va_list params)
18 char msg[4096];
19 int len = vsnprintf(msg, sizeof(msg), err, params);
20 if (len > sizeof(msg))
21 len = sizeof(msg);
23 write_in_full(fd, prefix, strlen(prefix));
24 write_in_full(fd, msg, len);
25 write_in_full(fd, "\n", 1);
28 static NORETURN void usage_builtin(const char *err, va_list params)
30 vreportf("usage: ", err, params);
31 exit(129);
34 static NORETURN void die_builtin(const char *err, va_list params)
36 vreportf("fatal: ", err, params);
37 exit(128);
40 static void error_builtin(const char *err, va_list params)
42 vreportf("error: ", err, params);
45 static void warn_builtin(const char *warn, va_list params)
47 vreportf("warning: ", warn, params);
50 /* If we are in a dlopen()ed .so write to a global variable would segfault
51 * (ugh), so keep things static. */
52 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
53 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
54 static void (*error_routine)(const char *err, va_list params) = error_builtin;
55 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
57 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
59 die_routine = routine;
62 void set_error_routine(void (*routine)(const char *err, va_list params))
64 error_routine = routine;
67 void NORETURN usagef(const char *err, ...)
69 va_list params;
71 va_start(params, err);
72 usage_routine(err, params);
73 va_end(params);
76 void NORETURN usage(const char *err)
78 usagef("%s", err);
81 void NORETURN die(const char *err, ...)
83 va_list params;
85 va_start(params, err);
86 die_routine(err, params);
87 va_end(params);
90 void NORETURN die_errno(const char *fmt, ...)
92 va_list params;
93 char fmt_with_err[1024];
94 char str_error[256], *err;
95 int i, j;
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;
110 str_error[j] = 0;
111 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
113 va_start(params, fmt);
114 die_routine(fmt_with_err, params);
115 va_end(params);
118 int error(const char *err, ...)
120 va_list params;
122 va_start(params, err);
123 error_routine(err, params);
124 va_end(params);
125 return -1;
128 void warning(const char *warn, ...)
130 va_list params;
132 va_start(params, warn);
133 warn_routine(warn, params);
134 va_end(params);