Thread (34 messages) flat view 34 messages, 3 authors, 2016-06-15
STALE3742d

[PATCH 22/24] Let usage() take a printf-style format

From: Jonathan Nieder <hidden>
Date: 2016-06-15 22:47:41
Subsystem: the rest · Maintainer: Linus Torvalds

merge-recursive and diff --no-index are not able to use usage()
because their usage strings depend on the circumstances in which
they are called.

Signed-off-by: Jonathan Nieder <redacted>
---
 git-compat-util.h |    2 +-
 usage.c           |   14 +++++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index ef60803..42048e7 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -188,7 +188,7 @@ extern char *gitbasename(char *);
 #include "compat/bswap.h"
 
 /* General helper functions */
-extern NORETURN void usage(const char *err);
+extern NORETURN void usage(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 c488f3a..f6d9ff5 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,13 @@ void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list param
 	die_routine = routine;
 }
 
-void usage(const char *err)
+void usage(const char *err, ...)
 {
-	usage_routine(err);
+	va_list params;
+
+	va_start(params, err);
+	usage_routine(err, params);
+	va_end(params);
 }
 
 void die(const char *err, ...)
-- 
1.6.5.2
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help