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

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

E-mail: