git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Petr Baudis <pasky@suse.cz>
To: Junio C Hamano <junkio@cox.net>
Cc: <git@vger.kernel.org>
Subject: [PATCH 05/12] Customizable error handlers
Date: Sat, 24 Jun 2006 04:34:38 +0200	[thread overview]
Message-ID: <20060624023438.32751.46867.stgit@machine.or.cz> (raw)
In-Reply-To: <20060624023429.32751.80619.stgit@machine.or.cz>

This patch makes the usage(), die() and error() handlers customizable.
Nothing in the git code itself uses that but many other libgit users
(like Git.pm) will.

This is implemented using the mutator functions primarily because you
cannot directly modifying global variables of libgit from a program that
dlopen()ed it, apparently. But having functions for that is a better API
anyway.

Signed-off-by: Petr Baudis <pasky@suse.cz>
---

 git-compat-util.h |    4 ++++
 usage.c           |   46 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 5d543d2..b3d4cf5 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -40,6 +40,10 @@ extern void usage(const char *err) NORET
 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 
+extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
+extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
+extern void set_error_routine(void (*routine)(const char *err, va_list params));
+
 #ifdef NO_MMAP
 
 #ifndef PROT_READ
diff --git a/usage.c b/usage.c
index 1fa924c..b781b00 100644
--- a/usage.c
+++ b/usage.c
@@ -12,20 +12,58 @@ static void report(const char *prefix, c
 	fputs("\n", stderr);
 }
 
-void usage(const char *err)
+void usage_builtin(const char *err)
 {
 	fprintf(stderr, "usage: %s\n", err);
 	exit(129);
 }
 
+void die_builtin(const char *err, va_list params)
+{
+	report("fatal: ", err, params);
+	exit(128);
+}
+
+void error_builtin(const char *err, va_list params)
+{
+	report("error: ", err, 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 void (*error_routine)(const char *err, va_list params) = error_builtin;
+
+void set_usage_routine(void (*routine)(const char *err) NORETURN)
+{
+	usage_routine = routine;
+}
+
+void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
+{
+	die_routine = routine;
+}
+
+void set_error_routine(void (*routine)(const char *err, va_list params))
+{
+	error_routine = routine;
+}
+
+
+void usage(const char *err)
+{
+	usage_routine(err);
+}
+
 void die(const char *err, ...)
 {
 	va_list params;
 
 	va_start(params, err);
-	report("fatal: ", err, params);
+	die_routine(err, params);
 	va_end(params);
-	exit(128);
 }
 
 int error(const char *err, ...)
@@ -33,7 +71,7 @@ int error(const char *err, ...)
 	va_list params;
 
 	va_start(params, err);
-	report("error: ", err, params);
+	error_routine(err, params);
 	va_end(params);
 	return -1;
 }

  parent reply	other threads:[~2006-06-24  2:35 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-24  2:34 [PATCH 01/12] Introduce Git.pm (v4) Petr Baudis
2006-06-24  2:34 ` [PATCH 02/12] Git.pm: Implement Git::exec_path() Petr Baudis
2006-06-24  2:34 ` [PATCH 03/12] Git.pm: Call external commands using execv_git_cmd() Petr Baudis
2006-06-24  2:34 ` [PATCH 04/12] Git.pm: Implement Git::version() Petr Baudis
2006-06-24  2:34 ` Petr Baudis [this message]
2006-06-24  2:34 ` [PATCH 06/12] Add Error.pm to the distribution Petr Baudis
2006-06-24  2:34 ` [PATCH 07/12] Git.pm: Better error handling Petr Baudis
2006-06-24  8:37   ` Junio C Hamano
2006-06-24 13:17     ` Petr Baudis
2006-06-25  1:13       ` Petr Baudis
2006-06-25  1:30       ` Junio C Hamano
2006-06-24  2:34 ` [PATCH 08/12] Git.pm: Handle failed commands' output Petr Baudis
2006-06-24  2:34 ` [PATCH 09/12] Git.pm: Enhance the command_pipe() mechanism Petr Baudis
2006-06-24  2:34 ` [PATCH 10/12] Git.pm: Implement options for the command interface Petr Baudis
2006-06-24  2:34 ` [PATCH 11/12] Git.pm: Add support for subdirectories inside of working copies Petr Baudis
2006-06-24  2:34 ` [PATCH 12/12] Convert git-mv to use Git.pm Petr Baudis
2006-06-24  2:39   ` Petr Baudis
2006-06-24  2:46 ` [PATCH 01/12] Introduce Git.pm (v4) Junio C Hamano
2006-06-24  3:14   ` Petr Baudis
2006-06-24  8:33   ` Junio C Hamano
2006-06-24 11:16     ` Petr Baudis
2006-06-24 11:52       ` Petr Baudis
2006-06-24 11:57       ` Junio C Hamano
2006-06-24 13:02         ` Petr Baudis
2006-06-25  3:12           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060624023438.32751.46867.stgit@machine.or.cz \
    --to=pasky@suse.cz \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).