Author: yamakenz
Date: Sat Aug 11 10:36:39 2007
New Revision: 4829

Modified:
   trunk/doc/COMPATIBILITY
   trunk/uim/uim-error.c
   trunk/uim/uim-internal.h

Log:
* This commit fix the broken exceptoin-based error handlings. Now
  uim_throw_error() and uim_fatal_error() are actually recovered
  without application termination

* uim/uim-internal.h
  - Include setjmp.h
  - (JMP_BUF, SETJMP, LONGJMP): Moved from uim-error.c
  - (uim_print_caught_error, uim_caught_fatal_error,
    uim_catch_error_begin_pre, uim_catch_error_begin_post): New function decl
  - (uim_catch_error_begin): New macro
  - (uim_catch_block_env): New variable decl
* uim/uim-error.c
  - Exclude setjmp.h
  - (JMP_BUF, SETJMP, LONGJMP): Move to uim-internal.h
  - (env, uim_catch_block_env): Replace static var env with
    public uim_catch_block_env
  - (uim_print_caught_error, uim_caught_fatal_error,
    uim_catch_error_begin_pre, uim_catch_error_begin_post): New
    function
  - (uim_catch_error_begin): Removed and replaced with the same name
    macro in uim-internal.h
  - (uim_throw_error): Follow the renaming of env
* doc/COMPATIBILITY
  - Update


Modified: trunk/doc/COMPATIBILITY
==============================================================================
--- trunk/doc/COMPATIBILITY     (original)
+++ trunk/doc/COMPATIBILITY     Sat Aug 11 10:36:39 2007
@@ -81,8 +81,8 @@
 Affects: uim developers
 Updates: internal C API
 Version: 1.5.0
-Revision: ac4814, ac4815
-Date: 2007-08-11
+Revision: ac4814, ac4815, ac4829
+Date: 2007-08-11, 2007-08-12
 Modifier: YamaKen
 Related: Fatal error handling helpers for plugin developers
         uim-scm API reorganization in uim 1.5.0

Modified: trunk/uim/uim-error.c
==============================================================================
--- trunk/uim/uim-error.c       (original)
+++ trunk/uim/uim-error.c       Sat Aug 11 10:36:39 2007
@@ -35,7 +35,6 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <setjmp.h>
 #include <stdio.h>
 #include <assert.h>
 
@@ -43,21 +42,12 @@
 #include "uim-internal.h"
 
 
-#if HAVE_SIGSETJMP
-#define JMP_BUF           sigjmp_buf
-#define SETJMP(env)       sigsetjmp((env), 1)
-#define LONGJMP(env, val) siglongjmp((env), (val))
-#else
-#define JMP_BUF           jmp_buf
-#define SETJMP(env)       setjmp(env)
-#define LONGJMP(env, val) longjmp((env), (val))
-#endif
 
 /* Immediately returns UIM_TRUE if uim is disabled by a fatal error. */
 
+JMP_BUF uim_catch_block_env;
 static uim_bool fatal_errored;
 static int guarded;
-static JMP_BUF env;
 static const char *err_msg;
 
 
@@ -70,32 +60,39 @@
   /* fatal_errored must not be cleared even if libuim is re-initialized. */
 }
 
-/* can be nested */
+void
+uim_print_caught_error(void)
+{
+  if (err_msg) {
+    fputs("ERROR: ", stderr);
+    if (fatal_errored)
+      fputs("fatal: ", stderr);
+    fputs(err_msg, stderr);
+    fputs("\n", stderr);
+  }
+}
+
 uim_bool
-uim_catch_error_begin(void)
+uim_caught_fatal_error(void)
 {
-  assert(guarded >= 0);
+  return fatal_errored;
+}
 
-  if (fatal_errored)
-    return UIM_TRUE;
+uim_bool
+uim_catch_error_begin_pre(void)
+{
+  assert(guarded >= 0);
 
-  if (!guarded++) {
-    if (SETJMP(env)) {
-      guarded = 0;
-
-      if (err_msg) {
-       fputs("ERROR: ", stderr);
-       if (fatal_errored)
-         fputs("fatal: ", stderr);
-       fputs(err_msg, stderr);
-       fputs("\n", stderr);
-      }
+  return !guarded++;
+}
 
-      return UIM_TRUE;
-    }
-  }
+uim_bool
+uim_catch_error_begin_post(void)
+{
+  guarded = 0;
+  uim_print_caught_error();
 
-  return UIM_FALSE;
+  return UIM_TRUE;
 }
 
 void
@@ -115,7 +112,7 @@
     exit(EXIT_FAILURE);
 
   err_msg = msg;
-  LONGJMP(env, guarded);
+  LONGJMP(uim_catch_block_env, guarded);
 }
 
 void

Modified: trunk/uim/uim-internal.h
==============================================================================
--- trunk/uim/uim-internal.h    (original)
+++ trunk/uim/uim-internal.h    Sat Aug 11 10:36:39 2007
@@ -35,6 +35,8 @@
 
 #include <config.h>
 
+#include <setjmp.h>
+
 #include "uim.h"
 #include "uim-scm.h"
 
@@ -42,6 +44,18 @@
 extern "C" {
 #endif
 
+
+#if HAVE_SIGSETJMP
+#define JMP_BUF           sigjmp_buf
+#define SETJMP(env)       sigsetjmp((env), 1)
+#define LONGJMP(env, val) siglongjmp((env), (val))
+#else
+#define JMP_BUF           jmp_buf
+#define SETJMP(env)       setjmp(env)
+#define LONGJMP(env, val) longjmp((env), (val))
+#endif
+
+
 struct uim_candidate_ {
   char *str;         /* candidate */
   char *heading_label;
@@ -106,11 +120,21 @@
   void (*switch_system_global_im_cb)(void *ptr, const char *name);
 };
 
-
+/* internal functions */
 void     uim_init_error(void);
-uim_bool uim_catch_error_begin(void);
-void     uim_catch_error_end(void);
-void     uim_throw_error(const char *msg);
+void     uim_print_caught_error(void);
+uim_bool uim_caught_fatal_error(void);
+uim_bool uim_catch_error_begin_pre(void);
+uim_bool uim_catch_error_begin_post(void);
+
+/* can be nested */
+#define uim_catch_error_begin()                                                
\
+  (uim_caught_fatal_error()                                            \
+   || (uim_catch_error_begin_pre()                                     \
+       && SETJMP(uim_catch_block_env)                                  \
+       && uim_catch_error_begin_post()))
+void    uim_catch_error_end(void);
+void    uim_throw_error(const char *msg);
 
 void uim_scm_init(const char *system_load_path);
 void uim_scm_quit(void);
@@ -127,6 +151,10 @@
 void uim_init_intl_subrs(void);
 
 uim_bool uim_issetugid(void);
+
+
+/* don't touch directly */
+extern JMP_BUF uim_catch_block_env;
 
 #ifdef __cplusplus
 }

Reply via email to