git's error handling can be hooked, unfortunally only by longjmp this way
authorChristian Thaeter <ct@pipapo.org>
Fri, 28 Dec 2007 23:33:46 +0000 (00:33 +0100)
committerChristian Thaeter <ct@pipapo.org>
Fri, 28 Dec 2007 23:33:46 +0000 (00:33 +0100)
src/ctgit.c

index f7cf4725f49e171522419425c6f456edff910c42..4b60f4e5ef7f7cdbd8f52f795030d5d6f598df03 100644 (file)
 #include "query.h"
 #include "options.h"
 
-#include "cwa.h"
+#include "git/git-compat-util.h"
+
+#include <cwa.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
+#include <setjmp.h>
 
 Html error_log;
+jmp_buf err_jmp;
+
+void
+ctgit_err_vargs (const char *err, va_list params)
+{
+  html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag ("br")));
+  longjmp (err_jmp, 0);
+}
+
+void
+ctgit_warn_vargs (const char *err, va_list params)
+{
+  html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag("br")));
+}
+
+
+static void
+ctgit_setup (void)
+{
+  set_die_routine (ctgit_err_vargs);
+  set_error_routine (ctgit_err_vargs);
+  set_warn_routine (ctgit_warn_vargs);
+}
+
 
 int
 main (int argc, char**argv)
@@ -44,6 +71,8 @@ main (int argc, char**argv)
   Html page;
   struct ctgit_query query;
 
+  ctgit_setup();
+
   error_log = html_list();
 
   clock_t timestat = clock();
@@ -55,6 +84,10 @@ main (int argc, char**argv)
   setenv ("SCRIPT_NAME", argv[0], 0);
   setenv ("CTGIT_CONFIG", CTGIT_CONFIG, 0);
 
+  /* we can only longjmp out of libgit's error handling */
+  if (setjmp (err_jmp))
+    goto error;
+
   if (ctgit_commandline_dispatch (argc, argv, &query))
     goto error;
 
@@ -100,9 +133,15 @@ main (int argc, char**argv)
                                               ),
                                          /*body*/
                                          html (
-                                               html_tag("div"),
-                                               ctgit_action_dispatch (&query)
-                                               ),
+                                               html (
+                                                     html_tag("div"),
+                                                     ctgit_action_dispatch (&query)
+                                                     ),
+                                               html(
+                                                    html_tag("div"),
+                                                    error_log
+                                                    )
+                                         ),
                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
                                          html_attr("xml:lang", "en"),
                                          html_attr("lang", "en")
@@ -116,7 +155,6 @@ main (int argc, char**argv)
   fprintf (stderr, "processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC);
 
   html_free (page);
-  html_free (error_log);
 
   ctgit_query_destroy (&query);