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