? registerErrorHandler.patch Index: ruby_xml_parser.c =================================================================== RCS file: /var/cvs/xml-tools/libxml-ruby/ruby_xml_parser.c,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 ruby_xml_parser.c --- ruby_xml_parser.c 22 Mar 2005 19:25:34 -0000 1.1.1.1 +++ ruby_xml_parser.c 5 Nov 2005 12:17:56 -0000 @@ -4,6 +4,9 @@ #include "libxml.h" +static VALUE libxml_xmlRubyErrorProc = Qnil; +static int id_call; + int ruby_xml_parser_count = 0; VALUE cXMLParser; VALUE eXMLParserParseError; @@ -848,6 +851,35 @@ } +VALUE +ruby_xml_parser_registerErrorHandler(int argc, VALUE* argv, VALUE self) { + VALUE old_block = libxml_xmlRubyErrorProc; + rb_scan_args(argc, argv, "0&", &libxml_xmlRubyErrorProc); + return(old_block); +} + +static void +libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg, + ...) +{ + va_list ap; + char str[1000]; + VALUE rstr; + + if (libxml_xmlRubyErrorProc == Qnil) { + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); + } else { + va_start(ap, msg); + if (vsnprintf(str, 999, msg, ap) >= 998) str[999] = 0; + va_end(ap); + + rstr = rb_str_new2(str); + rb_funcall2(libxml_xmlRubyErrorProc, id_call, 1, &rstr); + } +} + #define RUBY_XML_PARSER_ENABLED_INIT(func, method) \ rb_define_singleton_method(cXMLParser, method, \ ruby_xml_parser_enabled_##func##_q, 0); @@ -951,6 +983,7 @@ ruby_xml_parser_memory_used, 0); rb_define_singleton_method(cXMLParser, "new", ruby_xml_parser_new, 0); rb_define_singleton_method(cXMLParser, "string", ruby_xml_parser_new_string, 1); + rb_define_singleton_method(cXMLParser, "registerErrorHandler", ruby_xml_parser_registerErrorHandler, -1); rb_define_method(cXMLParser, "filename", ruby_xml_parser_filename_get, 0); rb_define_method(cXMLParser, "filename=", ruby_xml_parser_filename_set, 1); @@ -960,4 +993,9 @@ rb_define_method(cXMLParser, "parser_context", ruby_xml_parser_parser_context_get, 0); rb_define_method(cXMLParser, "string", ruby_xml_parser_str_get, 0); rb_define_method(cXMLParser, "string=", ruby_xml_parser_str_set, 1); + + // set up error handling + xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); + xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); + id_call = rb_intern("call"); }