Annotation of parser3/src/main/pa_globals.C, revision 1.106
1.15 paf 1: /** @file
1.16 paf 2: Parser: globals.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.89 paf 5: Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.16 paf 6:
1.106 ! paf 7: $Id: pa_globals.C,v 1.105 2002/01/23 09:47:02 paf Exp $
1.1 paf 8: */
9:
1.102 paf 10: #include "pa_config_includes.h"
11:
12: #ifdef XML
13: //#include "libxml/parser.h"
14: //#include "libxslt/xslt.h"
15: //#include "libxslt/libxslt.h"
16: #include "libxslt/extensions.h"
17: #include "libxslt/xsltutils.h"
1.103 paf 18: extern "C" {
1.102 paf 19: #include "libexslt/exslt.h"
1.103 paf 20: };
1.102 paf 21: #endif
22:
1.1 paf 23: #include "pa_globals.h"
1.32 paf 24: #include "pa_string.h"
25: #include "pa_hash.h"
1.42 paf 26: #include "pa_sql_driver_manager.h"
1.77 parser 27: #include "pa_dictionary.h"
1.100 paf 28: #include "pa_stylesheet_manager.h"
1.83 parser 29: #include "pa_sapi.h"
1.90 paf 30: #include "pa_cache_managers.h"
1.95 paf 31: #include "pa_charsets.h"
32: #include "pa_charset.h"
1.101 paf 33: #include "pa_threads.h"
1.84 parser 34:
1.86 paf 35: #ifdef DB2
1.83 parser 36: #include "pa_db_manager.h"
1.84 parser 37: #endif
1.1 paf 38:
1.8 paf 39: String *content_type_name;
1.75 parser 40: String *charset_name;
1.8 paf 41: String *body_name;
1.13 paf 42: String *value_name;
1.14 paf 43: String *expires_name;
44: String *path_name;
1.17 paf 45: String *name_name;
46: String *size_name;
47: String *text_name;
1.8 paf 48:
1.1 paf 49: String *exception_method_name;
1.38 paf 50: String *post_process_method_name;
1.1 paf 51:
1.39 paf 52: String *content_disposition_name;
53: String *content_disposition_filename_name;
54:
1.1 paf 55: String *unnamed_name;
56:
57: String *auto_method_name;
58: String *main_method_name;
59:
60: String *main_class_name;
61:
1.6 paf 62: String *result_var_name;
1.64 parser 63: String *match_var_name;
1.41 paf 64: String *string_pre_match_name;
1.40 paf 65: String *string_match_name;
1.41 paf 66: String *string_post_match_name;
1.6 paf 67:
1.79 parser 68: String *charsets_name;
1.36 paf 69: String *mime_types_name;
70: String *vfile_mime_type_name;
1.63 parser 71: String *origins_mode_name;
1.67 parser 72:
1.69 parser 73: String *class_path_name;
74:
1.67 parser 75: String *switch_data_name;
76: String *case_default_value;
77:
1.70 parser 78: String *sql_limit_name;
79: String *sql_offset_name;
80: String *sql_default_name;
81:
1.95 paf 82: String *charset_UTF8_name;
83:
1.71 parser 84: String *hash_default_element_name;
1.42 paf 85:
1.1 paf 86: Hash *untaint_lang_name2enum;
87:
1.97 paf 88: GdomeDOMImplementation *domimpl;
89:
1.95 paf 90: Charset *utf8_charset;
1.32 paf 91:
1.5 paf 92: short hex_value[0x100];
93:
94: static void setup_hex_value() {
1.68 parser 95: memset(hex_value, 0, sizeof(hex_value));
1.5 paf 96: hex_value['0'] = 0;
97: hex_value['1'] = 1;
98: hex_value['2'] = 2;
99: hex_value['3'] = 3;
100: hex_value['4'] = 4;
101: hex_value['5'] = 5;
102: hex_value['6'] = 6;
103: hex_value['7'] = 7;
104: hex_value['8'] = 8;
105: hex_value['9'] = 9;
106: hex_value['A'] = 10;
107: hex_value['B'] = 11;
108: hex_value['C'] = 12;
109: hex_value['D'] = 13;
110: hex_value['E'] = 14;
111: hex_value['F'] = 15;
112: hex_value['a'] = 10;
113: hex_value['b'] = 11;
114: hex_value['c'] = 12;
115: hex_value['d'] = 13;
116: hex_value['e'] = 14;
117: hex_value['f'] = 15;
118: }
1.1 paf 119:
1.99 paf 120: #ifdef XML
1.101 paf 121:
122: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
123:
124: struct XML_Generic_error_info {
125: pa_thread_t thread_id;
126: char *message;
127: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
128:
129: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
130: for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
131: XML_Generic_error_info *p=xml_generic_error_infos+i;
132: if(p->thread_id==thread_id)
133: return p;
134: }
135: return 0;
136: }
137:
1.99 paf 138: static void
1.101 paf 139: xmlParserGenericErrorFunc(void *ctx, const char *msg, ...) {
140: pa_thread_t thread_id=pa_get_thread_id();
141:
142: // infinitely looking for free slot to fill it
143: while(true) {
144: SYNCHRONIZED; // find+fill blocked
145:
146: // first try to get existing for this thread_id
147: XML_Generic_error_info *p=xml_generic_error_info(thread_id);
148: if(!p) { // occupy empty one
149: p=xml_generic_error_info(0);
150: if(!p) // wait for empty for it to appear
151: continue;
152: }
153:
1.102 paf 154: p->thread_id=thread_id;
1.101 paf 155: size_t offset=p->message?strlen(p->message):0;
156: p->message=(char *)realloc(p->message, offset+MAX_STRING);
157: if(!p->message)
158: SAPI::die(
159: "out of memory in 'xmlParserGenericErrorFunc', failed to reallocate to %u bytes",
160: offset+MAX_STRING);
161:
162: va_list args;
163: va_start(args, msg);
164: vsnprintf(p->message+offset, MAX_STRING, msg, args);
165: va_end(args);
166:
167: break;
168: }
169: }
170:
1.102 paf 171: bool xmlHaveGenericErrors() {
172: pa_thread_t thread_id=pa_get_thread_id();
173:
174: SYNCHRONIZED; // find blocked
175:
176: return xml_generic_error_info(thread_id)!=0;
177: }
178:
1.101 paf 179: const char *xmlGenericErrors() {
180: pa_thread_t thread_id=pa_get_thread_id();
181:
182: SYNCHRONIZED; // find+free blocked
183:
184: XML_Generic_error_info *p=xml_generic_error_info(thread_id);
185: if(!p) // no errors for our thread_id registered
186: return 0;
187:
188: const char *result=p->message;
189:
190: // free slot up
191: memset(p, 0, sizeof(*p));
192:
193: // it is up to caller to free it
194: return result;
1.99 paf 195: }
196: #endif
197:
1.83 parser 198: void pa_globals_destroy(void *) {
199: try {
1.96 paf 200: #ifdef XML
201: GdomeException exc;
202: gdome_di_unref (domimpl, &exc);
203: #endif
1.93 paf 204: if(cache_managers)
205: cache_managers->~Cache_managers();
1.95 paf 206:
207: charsets->~Charsets();
1.83 parser 208:
209: } catch(const Exception& e) {
210: SAPI::die("pa_globals_destroy failed: %s", e.comment());
211: }
212: }
213:
1.104 paf 214: /// @test // replace entities on load. WARNING: in sources there is a hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
1.27 paf 215: void pa_globals_init(Pool& pool) {
1.83 parser 216: pool.register_cleanup(pa_globals_destroy, 0);
217:
1.32 paf 218: #undef NEW
219: #define NEW new(pool)
220:
1.5 paf 221: // hex value
222: setup_hex_value();
223:
1.1 paf 224: // names
1.32 paf 225: content_type_name=NEW String(pool, CONTENT_TYPE_NAME);
1.75 parser 226: charset_name=NEW String(pool, CHARSET_NAME);
1.32 paf 227: body_name=NEW String(pool, BODY_NAME);
228: value_name=NEW String(pool, VALUE_NAME);
229: expires_name=NEW String(pool, EXPIRES_NAME);
230: path_name=NEW String(pool, PATH_NAME);
231: name_name=NEW String(pool, NAME_NAME);
232: size_name=NEW String(pool, SIZE_NAME);
233: text_name=NEW String(pool, TEXT_NAME);
234:
235: exception_method_name=NEW String(pool, EXCEPTION_METHOD_NAME);
1.38 paf 236: post_process_method_name=NEW String(pool, POST_PROCESS_METHOD_NAME);
1.39 paf 237:
238: content_disposition_name=NEW String(pool, CONTENT_DISPOSITION_NAME);
239: content_disposition_filename_name=NEW String(pool, CONTENT_DISPOSITION_FILENAME_NAME);
1.32 paf 240:
241: unnamed_name=NEW String(pool, UNNAMED_NAME);
242:
243: auto_method_name=NEW String(pool, AUTO_METHOD_NAME);
244: main_method_name=NEW String(pool, MAIN_METHOD_NAME);
245:
246: main_class_name=NEW String(pool, MAIN_CLASS_NAME);
1.6 paf 247:
1.32 paf 248: result_var_name=NEW String(pool, RESULT_VAR_NAME);
1.64 parser 249: match_var_name=NEW String(pool, MATCH_VAR_NAME);
1.41 paf 250: string_pre_match_name=NEW String(pool, STRING_PRE_MATCH_NAME);
1.40 paf 251: string_match_name=NEW String(pool, STRING_MATCH_NAME);
1.41 paf 252: string_post_match_name=NEW String(pool, STRING_POST_MATCH_NAME);
1.6 paf 253:
1.4 paf 254:
1.79 parser 255: charsets_name=NEW String(pool, CHARSETS_NAME);
1.36 paf 256: mime_types_name=NEW String(pool, MIME_TYPES_NAME);
257: vfile_mime_type_name=NEW String(pool, VFILE_MIME_TYPE_NAME);
1.63 parser 258: origins_mode_name=NEW String(pool, ORIGINS_MODE_NAME);
1.69 parser 259:
260: class_path_name=NEW String(pool, CLASS_PATH_NAME);
1.66 parser 261:
262: //^switch ^case
263: switch_data_name=NEW String(pool, SWITCH_DATA_NAME);
264: case_default_value=NEW String(pool, CASE_DEFAULT_VALUE);
1.70 parser 265:
266: // sql
267: sql_limit_name=NEW String(pool, SQL_LIMIT_NAME);
268: sql_offset_name=NEW String(pool, SQL_OFFSET_NAME);
269: sql_default_name=NEW String(pool, SQL_DEFAULT_NAME);
1.71 parser 270:
1.95 paf 271: // charsets
272: charset_UTF8_name=NEW String(pool, CHARSET_UTF8_NAME);
273:
1.71 parser 274: // hash
275: hash_default_element_name=NEW String(pool, HASH_DEFAULT_ELEMENT_NAME);
1.70 parser 276:
1.66 parser 277:
1.1 paf 278: // hashes
1.32 paf 279: untaint_lang_name2enum=NEW Hash(pool);
1.87 paf 280: #define ULN(cstr, LANG) \
281: untaint_lang_name2enum->put(*NEW String(pool, cstr), (int)String::UL_##LANG);
282: ULN("as-is", AS_IS);
283: ULN("file-spec", FILE_SPEC);
284: ULN("http-header", HTTP_HEADER);
285: ULN("mail-header", MAIL_HEADER);
286: ULN("uri", URI);
287: ULN("table", TABLE);
288: ULN("sql", SQL);
289: ULN("js", JS);
290: ULN("xml", XML);
291: ULN("html", HTML);
1.92 paf 292: ULN("optimized-html", HTML|String::UL_OPTIMIZE_BIT);
1.1 paf 293:
1.95 paf 294: // charsets
295: charsets=NEW Charsets(pool);
296: charsets->put(*charset_UTF8_name,
297: utf8_charset=NEW Charset(pool, *charset_UTF8_name, 0/*no file=system*/));
1.88 paf 298:
1.101 paf 299:
1.88 paf 300: // Status registration, must be initialized before all registrants
1.90 paf 301: cache_managers=NEW Cache_managers(pool);
1.42 paf 302:
1.73 parser 303: // SQL driver manager
1.90 paf 304: cache_managers->put(*NEW String(pool, "sql"),
305: SQL_driver_manager=NEW SQL_Driver_manager(pool));
1.74 parser 306:
1.86 paf 307: #ifdef DB2
1.83 parser 308: // DB driver manager
1.90 paf 309: cache_managers->put(*NEW String(pool, "db"),
310: DB_manager=NEW DB_Manager(pool));
1.83 parser 311: #endif
312:
1.76 parser 313: #ifdef XML
1.96 paf 314: // initializing xml libs
315:
316: /* First I get a DOMImplementation reference */
317: domimpl = gdome_di_mkref ();
318: /*
319: * Register the EXSLT extensions and the test module
320: */
321: exsltRegisterAll();
322: xsltRegisterTestModule();
323: xmlDefaultSAXHandlerInit();
324: /*
325: * disable CDATA from being built in the document tree
326: */
1.101 paf 327: // never added yet xmlDefaultSAXHandler.cdataBlock = NULL;
1.99 paf 328:
329: /*
330: * Initialization function for the XML parser.
331: * This is not reentrant. Call once before processing in case of
332: * use in multithreaded programs.
333: */
334: xmlInitParser();
1.100 paf 335:
336: // Bit in the loadsubset context field to tell to do ID/REFs lookups
337: xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
338: // Bit in the loadsubset context field to tell to do complete the elements attributes lists
339: // with the ones defaulted from the DTDs
340: //never added yet xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.99 paf 341:
1.104 paf 342: //regretfully this not only replaces entities on parse, but also on generate xmlSubstituteEntitiesDefault(1);
1.105 paf 343: // never switched this on xmlIndentTreeOutput=1;
1.104 paf 344:
1.101 paf 345: memset(xml_generic_error_infos, 0, sizeof(xml_generic_error_infos));
346: xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102 paf 347: xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105 paf 348: // FILE *f=fopen("y:\\xslt.log", "wt");
349: // xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.96 paf 350:
351: // XSLT stylesheet manager
1.90 paf 352: cache_managers->put(*NEW String(pool, "stylesheet"),
353: stylesheet_manager=NEW Stylesheet_manager(pool));
1.76 parser 354: #endif
1.1 paf 355: }
1.76 parser 356:
357: #if defined(XML) && defined(_MSC_VER)
1.96 paf 358: # define XML_LIBS "/parser3project/win32xml"
359: # pragma comment(lib, XML_LIBS "/glib/lib/libglib-1.3-11.lib")
360: # pragma comment(lib, XML_LIBS "/libxml2-2.4.12/win32/dsp/libxml2_so/libxml2.lib")
361: # pragma comment(lib, XML_LIBS "/libxslt-1.0.9/win32/dsp/libexslt_so/libexslt.lib")
1.106 ! paf 362: # pragma comment(lib, XML_LIBS "/libxslt-1.0.9/win32/dsp/libxslt_so/libxslt.lib")
1.76 parser 363: # ifdef _DEBUG
1.98 paf 364: # pragma comment(lib, XML_LIBS "/gdome2-0.7.0/win32/dsp/Debug/libgdome.lib")
1.76 parser 365: # else
1.98 paf 366: # pragma comment(lib, XML_LIBS "/gdome2-0.7.0/win32/dsp/Release/libgdome.lib")
1.76 parser 367: # endif
368: #endif
1.85 paf 369:
370:
1.86 paf 371: #if defined(DB2) && defined(_MSC_VER)
372: # define LIBDB2_WIN32_BUILD "/parser3project/win32db/lib"
1.85 paf 373: # ifdef _DEBUG
1.86 paf 374: # pragma comment(lib, LIBDB2_WIN32_BUILD "/debug/libdb.lib")
1.85 paf 375: # else
1.86 paf 376: # pragma comment(lib, LIBDB2_WIN32_BUILD "/release/libdb.lib")
1.85 paf 377: # endif
378: #endif
E-mail: