Annotation of parser3/src/main/pa_globals.C, revision 1.157

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.157   ! paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.113     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.133     paf         6: */
1.16      paf         7: 
1.157   ! paf         8: static const char* IDENT_GLOBALS_C="$Date: 2003/07/24 07:21:03 $";
1.1       paf         9: 
1.102     paf        10: #include "pa_config_includes.h"
                     11: 
                     12: #ifdef XML
1.157   ! paf        13: #include "libxml/xmlversion.h"
1.102     paf        14: #include "libxslt/extensions.h"
                     15: #include "libxslt/xsltutils.h"
1.116     paf        16: extern "C" {
1.102     paf        17: #include "libexslt/exslt.h"
1.116     paf        18: };
1.102     paf        19: #endif
                     20: 
1.157   ! paf        21: #include "pcre.h"
        !            22: 
1.1       paf        23: #include "pa_globals.h"
1.32      paf        24: #include "pa_string.h"
1.83      parser     25: #include "pa_sapi.h"
1.101     paf        26: #include "pa_threads.h"
1.84      parser     27: 
1.70      parser     28: 
1.157   ! paf        29: // defines
1.155     paf        30: 
1.157   ! paf        31: //#define PA_DEBUG_XML_GC_MEMORY
1.95      paf        32: 
1.157   ! paf        33: // globals
1.32      paf        34: 
1.5       paf        35: short hex_value[0x100];
1.111     paf        36: 
                     37: #ifdef XML
                     38: GdomeDOMImplementation *domimpl;
                     39: #endif
1.5       paf        40: 
                     41: static void setup_hex_value() {
1.68      parser     42:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        43:        hex_value['0'] = 0;     
                     44:        hex_value['1'] = 1;     
                     45:        hex_value['2'] = 2;     
                     46:        hex_value['3'] = 3;     
                     47:        hex_value['4'] = 4;     
                     48:        hex_value['5'] = 5;     
                     49:        hex_value['6'] = 6;     
                     50:        hex_value['7'] = 7;     
                     51:        hex_value['8'] = 8;     
                     52:        hex_value['9'] = 9;
                     53:        hex_value['A'] = 10;
                     54:        hex_value['B'] = 11;
                     55:        hex_value['C'] = 12;
                     56:        hex_value['D'] = 13;
                     57:        hex_value['E'] = 14;
                     58:        hex_value['F'] = 15;
                     59:        hex_value['a'] = 10;
                     60:        hex_value['b'] = 11;
                     61:        hex_value['c'] = 12;
                     62:        hex_value['d'] = 13;
                     63:        hex_value['e'] = 14;
                     64:        hex_value['f'] = 15;
                     65: }
1.1       paf        66: 
1.99      paf        67: #ifdef XML
1.101     paf        68: 
                     69: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
                     70: 
1.157   ! paf        71: class XML_Generic_error_info {
        !            72: public:
1.101     paf        73:        pa_thread_t thread_id;
1.157   ! paf        74:        char buf[MAX_STRING];
        !            75:        size_t used;
        !            76: public:
        !            77:        XML_Generic_error_info() {
        !            78:                reset();
        !            79:        }
        !            80:        void reset() { 
        !            81:                thread_id=0; 
        !            82:                buf[used=0]=0;
        !            83:        }
        !            84:        const char* get_and_reset() {
        !            85:                char* result=new(PointerFreeGC) char[used+1];
        !            86:                memcpy(result, buf, used+1);
        !            87:                reset();
        !            88:                return result;
        !            89:        }
1.101     paf        90: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
                     91: 
                     92: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
                     93:        for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
                     94:                XML_Generic_error_info *p=xml_generic_error_infos+i;
                     95:                if(p->thread_id==thread_id)
                     96:                        return p;
                     97:        }
                     98:        return 0;
                     99: }
                    100: 
1.99      paf       101: static void
1.157   ! paf       102: xmlParserGenericErrorFunc(void *ctx, const char* msg, ...) { 
        !           103:        //_asm int 3;
        !           104:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       105: 
                    106:        // infinitely looking for free slot to fill it
                    107:        while(true) {
                    108:                SYNCHRONIZED;  // find+fill blocked
                    109: 
                    110:                // first try to get existing for this thread_id
                    111:                XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    112:                if(!p) { // occupy empty one
                    113:                        p=xml_generic_error_info(0);
1.157   ! paf       114:                        if(!p) // wait for empty one to appear
1.101     paf       115:                                continue;
                    116:                }
                    117: 
1.102     paf       118:                p->thread_id=thread_id;
1.101     paf       119:                
                    120:                va_list args;
                    121:                va_start(args, msg);
1.157   ! paf       122:                p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
1.101     paf       123:                va_end(args);
                    124: 
                    125:                break;
                    126:        }
                    127: }
                    128: 
1.102     paf       129: bool xmlHaveGenericErrors() {
1.157   ! paf       130:        pa_thread_t thread_id=pa_get_thread_id();
1.102     paf       131: 
                    132:        SYNCHRONIZED;  // find blocked
                    133: 
                    134:        return xml_generic_error_info(thread_id)!=0;
                    135: }
                    136: 
1.157   ! paf       137: const char* xmlGenericErrors() {
        !           138:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       139: 
                    140:        SYNCHRONIZED;  // find+free blocked
                    141: 
                    142:        XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    143:        if(!p) // no errors for our thread_id registered
                    144:                return 0;
                    145: 
1.157   ! paf       146:        return p->get_and_reset();
1.99      paf       147: }
1.110     paf       148: 
                    149: /**
                    150:  * xmlFileMatchWithLocalhostEqDocumentRoot:
                    151:  * filename:  the URI for matching
                    152:  *
                    153:  * check if the URI matches an HTTP one
                    154:  *
                    155:  * Returns 1 if matches, 0 otherwise
                    156:  */
                    157: static int
1.157   ! paf       158: xmlFileMatchLocalhost(const char* filename) {
        !           159:        if (!strncmp(filename, "http://localhost", 16))
        !           160:                return(1);
        !           161:        return(0);
1.110     paf       162: }
                    163: 
                    164: 
                    165: /**
                    166:  * xmlFileOpenHttpLocalhost :
                    167:  * filename:  the URI for matching
                    168:  *
                    169:  * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    170:  *
                    171:  * input from FILE *, supports compressed input
                    172:  * if filename is " " then the standard input is used
                    173:  *
                    174:  * Returns an I/O context or NULL in case of error
                    175:  */
                    176: static void *
1.157   ! paf       177: xmlFileOpenLocalhost (const char* filename) {
        !           178:        //_asm int 3;
        !           179:        FILE *fd;
        !           180:        const char* documentRoot;
        !           181:        char path[1000];
        !           182:        
1.110     paf       183:        path[0]=0;
                    184:        strcat(path, (documentRoot=getenv("DOCUMENT_ROOT"))?documentRoot:".");
                    185:        strcat(path, &filename[16]);
1.157   ! paf       186:        
1.110     paf       187: #ifdef WIN32
1.157   ! paf       188:        fd = fopen(path, "rb");
1.110     paf       189: #else
1.157   ! paf       190:        fd = fopen(path, "r");
1.110     paf       191: #endif /* WIN32 */
1.157   ! paf       192:        return((void *) fd);
1.110     paf       193: }
                    194: 
1.150     paf       195: /**
                    196:  * xmlFileRead:
                    197:  * @context:  the I/O context
                    198:  * @buffer:  where to drop data
                    199:  * @len:  number of bytes to write
                    200:  *
                    201:  * Read @len bytes to @buffer from the I/O channel.
                    202:  *
                    203:  * Returns the number of bytes written
                    204:  */
                    205: static int
                    206: pa_xmlFileRead (void * context, char * buffer, int len) {
1.157   ! paf       207:        return(fread(&buffer[0], 1,  len, (FILE *) context));
1.150     paf       208: }
                    209: 
                    210: /**
                    211:  * xmlFileClose:
                    212:  * @context:  the I/O context
                    213:  *
                    214:  * Close an I/O channel
                    215:  */
                    216: static int
                    217: pa_xmlFileClose (void * context) {
1.157   ! paf       218:        return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );
1.150     paf       219: }
                    220: 
1.99      paf       221: #endif
                    222: 
1.83      parser    223: void pa_globals_destroy(void *) {
                    224:        try {
1.96      paf       225: #ifdef XML
                    226:                GdomeException exc;
                    227:                gdome_di_unref (domimpl, &exc);
                    228: #endif
1.83      parser    229:        } catch(const Exception& e) {
1.157   ! paf       230:                SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83      parser    231:        }
                    232: }
                    233: 
                    234: 
1.157   ! paf       235: #ifdef XML
        !           236: 
        !           237: static char *pa_GC_strdup(const char *s) {
        !           238:        if(!s)
        !           239:                return 0;
        !           240: 
        !           241:        size_t size=strlen(s)+1;
        !           242:        char *result=(char *)GC_malloc_atomic(size);
        !           243:        memcpy(result, s, size);
        !           244:        return result;
        !           245: }
        !           246: 
        !           247: #ifdef PA_DEBUG_XML_GC_MEMORY
        !           248: void *pa_look_for[]={(void*)0x8abe000,(void*)0x0,(void*)0x0,(void*)0x0,
        !           249:                        (void*)0x0,(void*)0x0,(void*)0x0,(void*)0x0};
        !           250: bool pa_looked(void*p) {
        !           251:        for(int i=0; i<8; i++)
        !           252:                if(pa_look_for[i]==p)
        !           253:                        return true;
        !           254:        return false;
        !           255: }
        !           256: static void* pa_gc_malloc_log(size_t size){
        !           257:        void *p=pa_gc_malloc(size);
        !           258:         fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
        !           259: //     if(pa_looked(p))
        !           260: //             fprintf(stderr,"catched debug malloc(%d)=0x%p\n", size, p);
        !           261:        return p;
        !           262:         
        !           263: }
        !           264: static void* pa_gc_malloc_atomic_log(size_t size){
        !           265:        void *p=pa_gc_malloc_atomic(size);
        !           266:         fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
        !           267: //     if(pa_looked(p))
        !           268: //             fprintf(stderr,"catched debug malloc atomic(%d)=0x%p\n", size, p);
        !           269:        return p;
        !           270: }
        !           271: static void* pa_gc_realloc_log(void *ptr, size_t size){
        !           272:        void *p=pa_gc_realloc(ptr, size);
        !           273:         fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
        !           274: //     if(pa_looked(p))
        !           275: //             fprintf(stderr,"catched debug realloc(%d)=0x%p\n", size, p);
        !           276:        return p;
        !           277: }
        !           278: //static void pa_gc_free_ignore(void *){}
        !           279: static void pa_gc_free_log(void *p){
        !           280:         fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
        !           281: //     if(pa_looked(p))
        !           282: //             fprintf(stderr,"catched debug free(0x%p)\n", p);
        !           283:         pa_gc_free(p);
        !           284: }
        !           285: #endif
        !           286: #endif
        !           287: 
        !           288: void pa_CORD_oom_fn(void) {
        !           289:        SAPI::abort("out of memory (while expanding string)");
        !           290: }
        !           291: 
        !           292: /**
        !           293:        @todo LIBXML_VERSION with xmlGcMemSetup will be 2.5.7
        !           294:        @todo gc: libltdl: substitute lt_dlmalloc & co
        !           295: */
        !           296: static void gc_substitute_memory_management_functions() {
        !           297:        // in libxml & libxslt
        !           298: #ifdef XML
        !           299:        // asking to use GC memory
        !           300: #if LIBXML_VERSION >= 20507
        !           301: #ifdef PA_DEBUG_XML_GC_MEMORY
        !           302:        xmlGcMemSetup(
        !           303:                /*xmlFreeFunc */pa_gc_free_log,
        !           304:                /*xmlMallocFunc */pa_gc_malloc_log,
        !           305:                /*xmlMallocFunc */pa_gc_malloc_atomic_log,
        !           306:                /*xmlReallocFunc */pa_gc_realloc_log,
        !           307:                /*xmlStrdupFunc */pa_GC_strdup);
        !           308: #else
        !           309:        xmlGcMemSetup(
        !           310:                /*xmlFreeFunc */pa_gc_free,
        !           311:                /*xmlMallocFunc */pa_gc_malloc,
        !           312:                /*xmlMallocFunc */pa_gc_malloc_atomic,
        !           313:                /*xmlReallocFunc */pa_gc_realloc,
        !           314:                /*xmlStrdupFunc */pa_GC_strdup);
        !           315: #endif
1.32      paf       316: 
1.157   ! paf       317: #else
        !           318:        xmlMemSetup(
        !           319:                /*xmlFreeFunc */pa_gc_free,
        !           320:                /*xmlMallocFunc */pa_gc_malloc,
        !           321:                /*xmlReallocFunc */pa_gc_realloc,
        !           322:                /*xmlStrdupFunc */pa_GC_strdup);
        !           323: #endif
1.5       paf       324: 
1.157   ! paf       325: #endif
1.141     paf       326: 
1.157   ! paf       327:        // pcre
        !           328:        pcre_malloc=pa_gc_malloc;
        !           329:        pcre_free=pa_gc_free;
1.135     paf       330: 
1.1       paf       331: 
1.157   ! paf       332:        // cord
        !           333:        CORD_oom_fn=pa_CORD_oom_fn;
        !           334: }
1.88      paf       335: 
1.157   ! paf       336: /**
        !           337:        @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
        !           338: */
        !           339: void pa_globals_init() {
        !           340:        // in various libraries
        !           341:        gc_substitute_memory_management_functions();
1.101     paf       342: 
1.157   ! paf       343:        // hex value
        !           344:        setup_hex_value();
1.74      parser    345: 
1.76      parser    346: #ifdef XML
1.96      paf       347:        // initializing xml libs
                    348: 
1.157   ! paf       349:        /* First get a DOMImplementation reference */
1.96      paf       350:        domimpl = gdome_di_mkref ();
1.157   ! paf       351:        /*
        !           352:        * Register the EXSLT extensions and the test module
        !           353:        */
        !           354:        exsltRegisterAll();
        !           355:        xsltRegisterTestModule();
        !           356:        xmlDefaultSAXHandlerInit();
        !           357:        /*
        !           358:        * disable CDATA from being built in the document tree
        !           359:        */
        !           360:        // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
        !           361:        
1.99      paf       362:        /*
                    363:         * Initialization function for the XML parser.
                    364:         * This is not reentrant. Call once before processing in case of
                    365:         * use in multithreaded programs.
                    366:        */
                    367:        xmlInitParser();
1.107     paf       368: 
                    369:        // 1. this is needed for proper parsing of stylesheets
                    370:        // there were a situation where honest entity ruined innocent xpath compilation
                    371:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    372:        // 2. when dom tree with entites goes under transform text nodes 
                    373:        // got [erroreosly] cut on first entity occurance
1.109     paf       374:        // --
1.107     paf       375:        // that is why this is:
                    376:        xmlSubstituteEntitiesDefault(1);
1.100     paf       377:        
                    378:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    379:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    380:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    381:        // with the ones defaulted from the DTDs 
1.157   ! paf       382:        xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       383: 
                    384:        // validate each document after load/create (?)
                    385:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       386: 
1.104     paf       387: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       388:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       389: 
1.101     paf       390:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       391:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       392: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    393: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       394: 
                    395:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    396:        xmlRegisterInputCallbacks(
                    397:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150     paf       398:                pa_xmlFileRead, pa_xmlFileClose);
1.157   ! paf       399: #endif
        !           400: }
        !           401: 
        !           402: #ifdef _MSC_VER
        !           403: 
        !           404: #ifndef PA_DEBUG_DISABLE_GC
        !           405: #      define GC_LIB "../../../../win32/gc"
        !           406: #      ifdef _DEBUG
        !           407: #              pragma comment(lib, GC_LIB "/Debug/gc.lib")
        !           408: #      else
        !           409: #              pragma comment(lib, GC_LIB "/Release/gc.lib")
        !           410: #      endif
1.96      paf       411: 
1.76      parser    412: #endif
                    413: 
1.157   ! paf       414: #ifdef XML
1.153     paf       415: #      define GNOME_LIBS "../../../../win32xml/win32/gnome"
1.131     paf       416: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    417: #      ifdef _DEBUG
1.157   ! paf       418: 
        !           419: #              ifdef LIBXML_STATIC
        !           420: #                      pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-debug/libxml2_a.lib")
        !           421: #              else
        !           422: #                      pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-debug/libxml2.lib")
        !           423: #              endif
        !           424: 
        !           425: #              ifdef LIBXSLT_STATIC
        !           426: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugStatic/libxslt.lib")
        !           427: #              else
        !           428: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugDynamic/libxslt.lib")
        !           429: #              endif
        !           430: #              ifdef LIBEXSLT_STATIC
        !           431: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugStatic/libexslt.lib")
        !           432: #              else
        !           433: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugDynamic/libexslt.lib")
        !           434: #              endif
        !           435: 
        !           436: #              ifdef LIBGDOME_STATIC
        !           437: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugStatic/libgdome.lib")
        !           438: #              else
        !           439: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugDynamic/libgdome.lib")
        !           440: #              endif
        !           441: 
        !           442: #else
        !           443: 
        !           444: #              ifdef LIBXML_STATIC
        !           445: #                      pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-release/libxml2_a.lib")
        !           446: #              else
        !           447: #                      pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-release/libxml2.lib")
        !           448: #              endif
        !           449: 
        !           450: #              ifdef LIBXSLT_STATIC
        !           451: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseStatic/libxslt.lib")
        !           452: #              else
        !           453: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseDynamic/libxslt.lib")
        !           454: #              endif
        !           455: #              ifdef LIBEXSLT_STATIC
        !           456: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseStatic/libexslt.lib")
        !           457: #              else
        !           458: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseDynamic/libexslt.lib")
        !           459: #              endif
        !           460: 
        !           461: #              ifdef LIBGDOME_STATIC
        !           462: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseStatic/libgdome.lib")
        !           463: #              else
        !           464: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseDynamic/libgdome.lib")
        !           465: #              endif
        !           466: 
1.85      paf       467: #      endif
1.157   ! paf       468: #endif
        !           469: 
1.85      paf       470: #endif

E-mail: