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

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.  2(paf       8:3): static const char* IDENT_GLOBALS_C="$Date: 2003/04/01 11:11:55 $";
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:): #endif
                    241:): 
1.152.2.1  paf       242: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    243: void pa_globals_init() {
1.32      paf       244: 
1.5       paf       245:        // hex value
                    246:        setup_hex_value();
1.74      parser    247: 
1.76      parser    248: #ifdef XML
1.96      paf       249: 
1.152.2.19.2.  (paf      250:):      // initializing xml libs
                    251:): 
                    252:): #ifndef PA_DEBUG_DISABLE_GC
                    253:):      // asking to use GC memory
                    254:):      xmlMemSetup(
          2(paf     255:3):             /*xmlFreeFunc */pa_gc_free,
                    256:3):             /*xmlMallocFunc */pa_gc_malloc,
                    257:3):             /*xmlReallocFunc */pa_gc_realloc,
          (paf      258:):              /*xmlStrdupFunc */pa_GC_strdup);
                    259:): #endif
                    260:): 
                    261:):      /* First get a DOMImplementation reference */
1.96      paf       262:        domimpl = gdome_di_mkref ();
1.152.2.19.2.  (paf      263:):      /*
                    264:):      * Register the EXSLT extensions and the test module
                    265:):      */
                    266:):      exsltRegisterAll();
                    267:):      xsltRegisterTestModule();
                    268:):      xmlDefaultSAXHandlerInit();
                    269:):      /*
                    270:):      * disable CDATA from being built in the document tree
                    271:):      */
                    272:):      // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
                    273:):      
1.99      paf       274:        /*
                    275:         * Initialization function for the XML parser.
                    276:         * This is not reentrant. Call once before processing in case of
                    277:         * use in multithreaded programs.
                    278:        */
                    279:        xmlInitParser();
1.107     paf       280: 
                    281:        // 1. this is needed for proper parsing of stylesheets
                    282:        // there were a situation where honest entity ruined innocent xpath compilation
                    283:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    284:        // 2. when dom tree with entites goes under transform text nodes 
                    285:        // got [erroreosly] cut on first entity occurance
1.109     paf       286:        // --
1.107     paf       287:        // that is why this is:
                    288:        xmlSubstituteEntitiesDefault(1);
1.100     paf       289:        
                    290:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    291:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    292:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    293:        // with the ones defaulted from the DTDs 
1.152.2.19.2.  (paf      294:):      xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       295: 
                    296:        // validate each document after load/create (?)
                    297:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       298: 
1.104     paf       299: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       300:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       301: 
1.101     paf       302:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       303:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       304: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    305: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       306: 
                    307:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    308:        xmlRegisterInputCallbacks(
                    309:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150     paf       310:                pa_xmlFileRead, pa_xmlFileClose);
1.76      parser    311: #endif
1.1       paf       312: }
1.76      parser    313: 
1.152.2.19.2.  0(paf     314:3): #ifdef _MSC_VER
                    315:3): 
                    316:3): #ifndef PA_DEBUG_DISABLE_GC
                    317:3): #   define GC_LIB "/parser3project/win32/gc"
                    318:3): #   ifdef _DEBUG
                    319:3): #           pragma comment(lib, GC_LIB "/Debug/gc.lib")
                    320:3): #   else
                    321:3): #           pragma comment(lib, GC_LIB "/Release/gc.lib")
                    322:3): #   endif
                    323:3): 
                    324:3): #endif
                    325:3): 
                    326:3): #ifdef XML
1.132     paf       327: #      define GNOME_LIBS "/parser3project/win32xml/win32/gnome"
1.131     paf       328: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    329: #      ifdef _DEBUG
1.152.2.19.2.  0(paf     330:3): 
                    331:3): #           ifdef LIBXML_STATIC
                    332:3): #                   pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_DebugStatic/libxml2.lib")
                    333:3): #           else
                    334:3): #                   pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_DebugDynamic/libxml2.lib")
                    335:3): #           endif
                    336:3): 
                    337:3): #           ifdef LIBXSLT_STATIC
                    338:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugStatic/libxslt.lib")
                    339:3): #           else
                    340:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugDynamic/libxslt.lib")
                    341:3): #           endif
                    342:3): #           ifdef LIBEXSLT_STATIC
                    343:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugStatic/libexslt.lib")
                    344:3): #           else
                    345:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugDynamic/libexslt.lib")
                    346:3): #           endif
                    347:3): 
                    348:3): #           ifdef LIBGDOME_STATIC
                    349:3): #                   pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugStatic/libgdome.lib")
                    350:3): #           else
                    351:3): #                   pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugDynamic/libgdome.lib")
                    352:3): #           endif
                    353:3): 
                    354:3): #else
                    355:3): 
                    356:3): #           ifdef LIBXML_STATIC
                    357:3): #                   pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_ReleaseStatic/libxml2.lib")
                    358:3): #           else
                    359:3): #                   pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/lixml2_ReleaseDynamic/libxml2.lib")
                    360:3): #           endif
                    361:3): 
                    362:3): #           ifdef LIBXSLT_STATIC
                    363:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseStatic/libxslt.lib")
                    364:3): #           else
                    365:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseDynamic/libxslt.lib")
                    366:3): #           endif
                    367:3): #           ifdef LIBEXSLT_STATIC
                    368:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseStatic/libexslt.lib")
                    369:3): #           else
                    370:3): #                   pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseDynamic/libexslt.lib")
                    371:3): #           endif
                    372:3): 
                    373:3): #           ifdef LIBGDOME_STATIC
                    374:3): #                   pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseStatic/libgdome.lib")
                    375:3): #           else
                    376:3): #                   pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseDynamic/libgdome.lib")
                    377:3): #           endif
                    378:3): 
1.85      paf       379: #      endif
1.152.2.19.2.  0(paf     380:3): #endif
                    381:3): 
1.85      paf       382: #endif

E-mail: