From b875036e5a2ab569a2123abe9ebfe25258227951 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:44 +0200 Subject: Introduce die_errno() that appends strerror(errno) to die() There are many calls to die() that do, or should, report strerror(errno) to indicate how the syscall they guard failed. Introduce a small helper function for this case. Note: - POSIX says vsnprintf can modify errno in some unlikely cases, so we have to use errno early. - We take some care to pass the original format to die_routine(), in case someone wants to call die_errno() with custom format characters. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- usage.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'usage.c') diff --git a/usage.c b/usage.c index 820d09f92b..fd936a1aeb 100644 --- a/usage.c +++ b/usage.c @@ -60,6 +60,18 @@ void die(const char *err, ...) va_end(params); } +void die_errno(const char *fmt, ...) +{ + va_list params; + char fmt_with_err[1024]; + + snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno)); + + va_start(params, fmt); + die_routine(fmt_with_err, params); + va_end(params); +} + int error(const char *err, ...) { va_list params; -- cgit 1.2.3-korg From f8b5a8e13cb4d60c8b630f92a8f07590ef218ec5 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 27 Jun 2009 17:58:45 +0200 Subject: die_errno(): double % in strerror() output just in case [tr: handle border case where % is placed at end of buffer] Signed-off-by: Junio C Hamano Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- usage.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'usage.c') diff --git a/usage.c b/usage.c index fd936a1aeb..b6aea45280 100644 --- a/usage.c +++ b/usage.c @@ -64,8 +64,24 @@ void die_errno(const char *fmt, ...) { va_list params; char fmt_with_err[1024]; - - snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno)); + char str_error[256], *err; + int i, j; + + err = strerror(errno); + for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) { + if ((str_error[j++] = err[i++]) != '%') + continue; + if (j < sizeof(str_error) - 1) { + str_error[j++] = '%'; + } else { + /* No room to double the '%', so we overwrite it with + * '\0' below */ + j--; + break; + } + } + str_error[j] = 0; + snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error); va_start(params, fmt); die_routine(fmt_with_err, params); -- cgit 1.2.3-korg From a4f3131c07c1f601be1e24b2143ca7e2deea09b5 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Wed, 30 Sep 2009 18:05:49 +0000 Subject: increase portability of NORETURN declarations Some compilers (including at least MSVC) support NORETURN on function declarations, but only before the function-name. This patch makes it possible to define NORETURN to something meaningful for those compilers. Signed-off-by: Erik Faye-Lund Signed-off-by: Jeff King --- git-compat-util.h | 8 ++++---- index-pack.c | 4 ++-- usage.c | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'usage.c') diff --git a/git-compat-util.h b/git-compat-util.h index 8d6e29cdea..bac376f4c7 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -186,13 +186,13 @@ extern char *gitbasename(char *); #include "compat/bswap.h" /* General helper functions */ -extern void usage(const char *err) NORETURN; -extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); -extern void die_errno(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); +extern NORETURN void usage(const char *err); +extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); +extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); -extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); +extern void set_die_routine(NORETURN void (*routine)(const char *err, va_list params)); extern int prefixcmp(const char *str, const char *prefix); extern time_t tm_to_time_t(const struct tm *tm); diff --git a/index-pack.c b/index-pack.c index 340074fc79..b4f8278659 100644 --- a/index-pack.c +++ b/index-pack.c @@ -206,8 +206,8 @@ static void parse_pack_header(void) use(sizeof(struct pack_header)); } -static void bad_object(unsigned long offset, const char *format, - ...) NORETURN __attribute__((format (printf, 2, 3))); +static NORETURN void bad_object(unsigned long offset, const char *format, + ...) __attribute__((format (printf, 2, 3))); static void bad_object(unsigned long offset, const char *format, ...) { diff --git a/usage.c b/usage.c index b6aea45280..0555ce6ccd 100644 --- a/usage.c +++ b/usage.c @@ -36,12 +36,12 @@ static void warn_builtin(const char *warn, va_list params) /* If we are in a dlopen()ed .so write to a global variable would segfault * (ugh), so keep things static. */ -static void (*usage_routine)(const char *err) NORETURN = usage_builtin; -static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin; +static NORETURN void (*usage_routine)(const char *err) = usage_builtin; +static NORETURN void (*die_routine)(const char *err, va_list params) = die_builtin; static void (*error_routine)(const char *err, va_list params) = error_builtin; static void (*warn_routine)(const char *err, va_list params) = warn_builtin; -void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN) +void set_die_routine(NORETURN void (*routine)(const char *err, va_list params)) { die_routine = routine; } -- cgit 1.2.3-korg From 18660bc96ec0419cc096a53998d3197f2b905e8a Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Wed, 30 Sep 2009 18:05:50 +0000 Subject: add NORETURN_PTR for function pointers Some compilers (including at least MSVC and ARM RVDS) supports NORETURN on function declarations, but not on function pointers. This patch makes it possible to define NORETURN for these compilers, by splitting the NORETURN macro into two - one for function declarations and one for function pointers. Signed-off-by: Erik Faye-Lund Signed-off-by: Jeff King --- git-compat-util.h | 4 +++- usage.c | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'usage.c') diff --git a/git-compat-util.h b/git-compat-util.h index bac376f4c7..ef60803384 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -176,8 +176,10 @@ extern char *gitbasename(char *); #ifdef __GNUC__ #define NORETURN __attribute__((__noreturn__)) +#define NORETURN_PTR __attribute__((__noreturn__)) #else #define NORETURN +#define NORETURN_PTR #ifndef __attribute__ #define __attribute__(x) #endif @@ -192,7 +194,7 @@ extern NORETURN void die_errno(const char *err, ...) __attribute__((format (prin extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); -extern void set_die_routine(NORETURN void (*routine)(const char *err, va_list params)); +extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params)); extern int prefixcmp(const char *str, const char *prefix); extern time_t tm_to_time_t(const struct tm *tm); diff --git a/usage.c b/usage.c index 0555ce6ccd..c488f3afcd 100644 --- a/usage.c +++ b/usage.c @@ -36,12 +36,12 @@ static void warn_builtin(const char *warn, va_list params) /* If we are in a dlopen()ed .so write to a global variable would segfault * (ugh), so keep things static. */ -static NORETURN void (*usage_routine)(const char *err) = usage_builtin; -static NORETURN void (*die_routine)(const char *err, va_list params) = die_builtin; +static NORETURN_PTR void (*usage_routine)(const char *err) = usage_builtin; +static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin; static void (*error_routine)(const char *err, va_list params) = error_builtin; static void (*warn_routine)(const char *err, va_list params) = warn_builtin; -void set_die_routine(NORETURN void (*routine)(const char *err, va_list params)) +void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params)) { die_routine = routine; } -- cgit 1.2.3-korg From 64b1cb74f8312c0a43ce32f51097172efc69355a Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Mon, 9 Nov 2009 09:05:02 -0600 Subject: Introduce usagef() that takes a printf-style format Some new callers would want to use printf-like formatting, when issuing their usage messages. An option is to change usage() itself also be like printf(), which would make it similar to die() and warn(). But usage() is typically fixed, as opposed to die() and warn() that gives diagnostics depending on the situation. Indeed, the majority of strings given by existing callsites to usage() are fixed strings. If we were to make usage() take printf-style format, they all need to be changed to have "%s" as their first argument. So instead, introduce usagef() so that limited number of callers can use it. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- git-compat-util.h | 1 + usage.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'usage.c') diff --git a/git-compat-util.h b/git-compat-util.h index ef60803384..5c596875c2 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -189,6 +189,7 @@ extern char *gitbasename(char *); /* General helper functions */ extern NORETURN void usage(const char *err); +extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index c488f3afcd..e307e01b99 100644 --- a/usage.c +++ b/usage.c @@ -12,9 +12,9 @@ static void report(const char *prefix, const char *err, va_list params) fprintf(stderr, "%s%s\n", prefix, msg); } -static NORETURN void usage_builtin(const char *err) +static NORETURN void usage_builtin(const char *err, va_list params) { - fprintf(stderr, "usage: %s\n", err); + report("usage: ", err, params); exit(129); } @@ -36,7 +36,7 @@ static void warn_builtin(const char *warn, va_list params) /* If we are in a dlopen()ed .so write to a global variable would segfault * (ugh), so keep things static. */ -static NORETURN_PTR void (*usage_routine)(const char *err) = usage_builtin; +static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin; static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin; static void (*error_routine)(const char *err, va_list params) = error_builtin; static void (*warn_routine)(const char *err, va_list params) = warn_builtin; @@ -46,9 +46,18 @@ void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list param die_routine = routine; } +void usagef(const char *err, ...) +{ + va_list params; + + va_start(params, err); + usage_routine(err, params); + va_end(params); +} + void usage(const char *err) { - usage_routine(err); + usagef("%s", err); } void die(const char *err, ...) -- cgit 1.2.3-korg From 625a860cb73b259683a4285705ac17e92298d5d5 Mon Sep 17 00:00:00 2001 From: Björn Gustavsson Date: Sun, 22 Nov 2009 22:19:53 +0100 Subject: Fix truncated usage messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The usage messages for some commands (such as 'git diff-tree') are truncated because they don't fit in a fixed buffer of 1024 bytes. It would be tempting to eliminate the buffer and the problem once and for all by doing the output in three steps, but doing so could (according to commit d048a96e) increase the likelyhood of messing up the display. So we just increase the size of the buffer. Signed-off-by: Björn Gustavsson Signed-off-by: Junio C Hamano --- usage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'usage.c') diff --git a/usage.c b/usage.c index e307e01b99..79856a2b9f 100644 --- a/usage.c +++ b/usage.c @@ -7,7 +7,7 @@ static void report(const char *prefix, const char *err, va_list params) { - char msg[1024]; + char msg[4096]; vsnprintf(msg, sizeof(msg), err, params); fprintf(stderr, "%s%s\n", prefix, msg); } -- cgit 1.2.3-korg