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

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.  (paf        8:): static const char* IDENT_GLOBALS_C="$Date: 2003/03/28 07:51:38 $";
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.  (paf       81:):      const char* get_and_reset() {
                     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.17  paf       142:        return p->get_and_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:): 
                    269:): static Hash<int, bool> valid_ptrs;
                    270:): static void* add_valid_ptr(void* ptr) {
                    271:):      valid_ptrs.put((int)ptr, true);
                    272:):      return ptr;
                    273:): }
                    274:): static void* check_ptr(void* ptr) {
                    275:):      if(!valid_ptrs.get((int)ptr))
                    276:):              _asm int 3;
                    277:):      return ptr;
                    278:): }
                    279:): 
                    280:): static int pa_debug_xml=1;
                    281:): static void* pa_debug_GC_malloc(size_t size) {
                    282:):      if(pa_debug_xml)
                    283:):              return add_valid_ptr(GC_malloc(size));
                    284:):      else
                    285:):              return GC_malloc(size);
                    286:): }
                    287:): 
                    288:): static void *pa_debug_GC_realloc(void *ptr, size_t size) {
                    289:):      //printf("realloc: 0x%p -> %u\n", ptr, size);
                    290:):      if(pa_debug_xml)
                    291:):              return add_valid_ptr(GC_realloc(check_ptr(ptr), size));
                    292:):      else
                    293:):              return GC_realloc(ptr, size);
                    294:): }
                    295:): 
                    296:): static void pa_debug_GC_free(void*) {
                    297:):      // ignore free
                    298:): }
                    299:): 
                    300:): #endif
                    301:): 
                    302:): 
                    303:): #endif
                    304:): 
1.152.2.1  paf       305: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    306: void pa_globals_init() {
1.32      paf       307: 
1.5       paf       308:        // hex value
                    309:        setup_hex_value();
1.74      parser    310: 
1.76      parser    311: #ifdef XML
1.96      paf       312: 
1.152.2.19.2.  (paf      313:):      // initializing xml libs
                    314:): 
                    315:): #ifndef PA_DEBUG_DISABLE_GC
                    316:): 
                    317:):      // asking to use GC memory
                    318:): #ifndef PA_DEBUG_XML_GC_MEMORY
                    319:):      xmlMemSetup(
                    320:):              /*xmlFreeFunc */GC_free,
                    321:):              /*xmlMallocFunc */GC_malloc,
                    322:):              /*xmlReallocFunc */GC_realloc,
                    323:):              /*xmlStrdupFunc */pa_GC_strdup);
                    324:): #else
                    325:):      xmlMemSetup(
                    326:):              /*xmlFreeFunc */pa_debug_GC_free,
                    327:):              /*xmlMallocFunc */pa_debug_GC_malloc,
                    328:):              /*xmlReallocFunc */pa_debug_GC_realloc,
                    329:):              /*xmlStrdupFunc */pa_debug_xmlStrdup);
                    330:): #endif
                    331:): 
                    332:): #endif
                    333:): 
                    334:):      /* First get a DOMImplementation reference */
1.96      paf       335:        domimpl = gdome_di_mkref ();
1.152.2.19.2.  (paf      336:):      /*
                    337:):      * Register the EXSLT extensions and the test module
                    338:):      */
                    339:):      exsltRegisterAll();
                    340:):      xsltRegisterTestModule();
                    341:):      xmlDefaultSAXHandlerInit();
                    342:):      /*
                    343:):      * disable CDATA from being built in the document tree
                    344:):      */
                    345:):      // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
                    346:):      
1.99      paf       347:        /*
                    348:         * Initialization function for the XML parser.
                    349:         * This is not reentrant. Call once before processing in case of
                    350:         * use in multithreaded programs.
                    351:        */
                    352:        xmlInitParser();
1.107     paf       353: 
                    354:        // 1. this is needed for proper parsing of stylesheets
                    355:        // there were a situation where honest entity ruined innocent xpath compilation
                    356:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    357:        // 2. when dom tree with entites goes under transform text nodes 
                    358:        // got [erroreosly] cut on first entity occurance
1.109     paf       359:        // --
1.107     paf       360:        // that is why this is:
                    361:        xmlSubstituteEntitiesDefault(1);
1.100     paf       362:        
                    363:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    364:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    365:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    366:        // with the ones defaulted from the DTDs 
1.152.2.19.2.  (paf      367:):      xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       368: 
                    369:        // validate each document after load/create (?)
                    370:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       371: 
1.104     paf       372: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       373:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       374: 
1.101     paf       375:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       376:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       377: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    378: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       379: 
                    380:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    381:        xmlRegisterInputCallbacks(
                    382:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150     paf       383:                pa_xmlFileRead, pa_xmlFileClose);
1.76      parser    384: #endif
1.1       paf       385: }
1.76      parser    386: 
                    387: #if defined(XML) && defined(_MSC_VER)
1.132     paf       388: #      define GNOME_LIBS "/parser3project/win32xml/win32/gnome"
1.131     paf       389: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    390: #      ifdef _DEBUG
1.131     paf       391: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_debug/libxml2.lib")
                    392: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_debug/libexslt.lib")
                    393: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_debug/libxslt.lib")
                    394: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Debug/libgdome.lib")
1.76      parser    395: #      else
1.131     paf       396: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_release/libxml2.lib")
                    397: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_release/libexslt.lib")
                    398: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_release/libxslt.lib")
                    399: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Release/libgdome.lib")
1.85      paf       400: #      endif
                    401: #endif

E-mail: