Annotation of parser3/src/main/pa_globals.C, revision 1.152.2.19.2.10
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. 0(paf 8:3): static const char* IDENT_GLOBALS_C="$Date: 2003/03/31 08:35:45 $";
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:):
0(paf 269:3): static const int PA_CHECKBUF_SIZE=100000000;
270:3): static bool maybe_valid_ptrs[PA_CHECKBUF_SIZE];
271:3):
(paf 272:): static void* add_valid_ptr(void* ptr) {
0(paf 273:3): uint h=hash_code((int)ptr);
274:3): maybe_valid_ptrs[h%PA_CHECKBUF_SIZE]=true;
(paf 275:): return ptr;
276:): }
277:): static void* check_ptr(void* ptr) {
0(paf 278:3): uint h=hash_code((uint)ptr);
279:3): if(!maybe_valid_ptrs[h%PA_CHECKBUF_SIZE])
(paf 280:): _asm int 3;
281:): return ptr;
282:): }
283:):
0(paf 284:3): static bool pa_debug_xml=true;
(paf 285:): static void* pa_debug_GC_malloc(size_t size) {
286:): if(pa_debug_xml)
287:): return add_valid_ptr(GC_malloc(size));
288:): else
289:): return GC_malloc(size);
290:): }
291:):
292:): static void *pa_debug_GC_realloc(void *ptr, size_t size) {
0(paf 293:3): GC_is_visible(ptr);
(paf 294:): //printf("realloc: 0x%p -> %u\n", ptr, size);
295:): if(pa_debug_xml)
296:): return add_valid_ptr(GC_realloc(check_ptr(ptr), size));
297:): else
298:): return GC_realloc(ptr, size);
299:): }
300:):
301:): static void pa_debug_GC_free(void*) {
302:): // ignore free
303:): }
304:):
305:): #endif
306:):
307:):
308:): #endif
309:):
1.152.2.1 paf 310: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
311: void pa_globals_init() {
1.32 paf 312:
1.5 paf 313: // hex value
314: setup_hex_value();
1.74 parser 315:
1.76 parser 316: #ifdef XML
1.96 paf 317:
1.152.2.19.2. (paf 318:): // initializing xml libs
319:):
320:): #ifndef PA_DEBUG_DISABLE_GC
321:):
322:): // asking to use GC memory
323:): #ifndef PA_DEBUG_XML_GC_MEMORY
324:): xmlMemSetup(
325:): /*xmlFreeFunc */GC_free,
326:): /*xmlMallocFunc */GC_malloc,
327:): /*xmlReallocFunc */GC_realloc,
328:): /*xmlStrdupFunc */pa_GC_strdup);
329:): #else
330:): xmlMemSetup(
331:): /*xmlFreeFunc */pa_debug_GC_free,
332:): /*xmlMallocFunc */pa_debug_GC_malloc,
333:): /*xmlReallocFunc */pa_debug_GC_realloc,
334:): /*xmlStrdupFunc */pa_debug_xmlStrdup);
335:): #endif
336:):
337:): #endif
338:):
339:): /* First get a DOMImplementation reference */
1.96 paf 340: domimpl = gdome_di_mkref ();
1.152.2.19.2. (paf 341:): /*
342:): * Register the EXSLT extensions and the test module
343:): */
344:): exsltRegisterAll();
345:): xsltRegisterTestModule();
346:): xmlDefaultSAXHandlerInit();
347:): /*
348:): * disable CDATA from being built in the document tree
349:): */
350:): // never added yet xmlDefaultSAXHandler.cdataBlock = NULL;
351:):
1.99 paf 352: /*
353: * Initialization function for the XML parser.
354: * This is not reentrant. Call once before processing in case of
355: * use in multithreaded programs.
356: */
357: xmlInitParser();
1.107 paf 358:
359: // 1. this is needed for proper parsing of stylesheets
360: // there were a situation where honest entity ruined innocent xpath compilation
361: // doc says "you sould turn it on on stylesheet load" without deepening into details
362: // 2. when dom tree with entites goes under transform text nodes
363: // got [erroreosly] cut on first entity occurance
1.109 paf 364: // --
1.107 paf 365: // that is why this is:
366: xmlSubstituteEntitiesDefault(1);
1.100 paf 367:
368: // Bit in the loadsubset context field to tell to do ID/REFs lookups
369: xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
370: // Bit in the loadsubset context field to tell to do complete the elements attributes lists
371: // with the ones defaulted from the DTDs
1.152.2.19.2. (paf 372:): xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138 paf 373:
374: // validate each document after load/create (?)
375: //xmlDoValidityCheckingDefaultValue = 1;
1.99 paf 376:
1.104 paf 377: //regretfully this not only replaces entities on parse, but also on generate xmlSubstituteEntitiesDefault(1);
1.105 paf 378: // never switched this on xmlIndentTreeOutput=1;
1.104 paf 379:
1.101 paf 380: xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102 paf 381: xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105 paf 382: // FILE *f=fopen("y:\\xslt.log", "wt");
383: // xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110 paf 384:
385: // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
386: xmlRegisterInputCallbacks(
387: xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150 paf 388: pa_xmlFileRead, pa_xmlFileClose);
1.76 parser 389: #endif
1.1 paf 390: }
1.76 parser 391:
1.152.2.19.2. 0(paf 392:3): #ifdef _MSC_VER
393:3):
394:3): #ifndef PA_DEBUG_DISABLE_GC
395:3): # define GC_LIB "/parser3project/win32/gc"
396:3): # ifdef _DEBUG
397:3): # pragma comment(lib, GC_LIB "/Debug/gc.lib")
398:3): # else
399:3): # pragma comment(lib, GC_LIB "/Release/gc.lib")
400:3): # endif
401:3):
402:3): #endif
403:3):
404:3): #ifdef XML
1.132 paf 405: # define GNOME_LIBS "/parser3project/win32xml/win32/gnome"
1.131 paf 406: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76 parser 407: # ifdef _DEBUG
1.152.2.19.2. 0(paf 408:3):
409:3): # ifdef LIBXML_STATIC
410:3): # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_DebugStatic/libxml2.lib")
411:3): # else
412:3): # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_DebugDynamic/libxml2.lib")
413:3): # endif
414:3):
415:3): # ifdef LIBXSLT_STATIC
416:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugStatic/libxslt.lib")
417:3): # else
418:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugDynamic/libxslt.lib")
419:3): # endif
420:3): # ifdef LIBEXSLT_STATIC
421:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugStatic/libexslt.lib")
422:3): # else
423:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugDynamic/libexslt.lib")
424:3): # endif
425:3):
426:3): # ifdef LIBGDOME_STATIC
427:3): # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugStatic/libgdome.lib")
428:3): # else
429:3): # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugDynamic/libgdome.lib")
430:3): # endif
431:3):
432:3): #else
433:3):
434:3): # ifdef LIBXML_STATIC
435:3): # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/libxml2_ReleaseStatic/libxml2.lib")
436:3): # else
437:3): # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/dsp/lixml2_ReleaseDynamic/libxml2.lib")
438:3): # endif
439:3):
440:3): # ifdef LIBXSLT_STATIC
441:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseStatic/libxslt.lib")
442:3): # else
443:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseDynamic/libxslt.lib")
444:3): # endif
445:3): # ifdef LIBEXSLT_STATIC
446:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseStatic/libexslt.lib")
447:3): # else
448:3): # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseDynamic/libexslt.lib")
449:3): # endif
450:3):
451:3): # ifdef LIBGDOME_STATIC
452:3): # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseStatic/libgdome.lib")
453:3): # else
454:3): # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseDynamic/libgdome.lib")
455:3): # endif
456:3):
1.85 paf 457: # endif
1.152.2.19.2. 0(paf 458:3): #endif
459:3):
1.85 paf 460: #endif
E-mail: