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

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

E-mail: