Annotation of parser3/src/classes/op.C, revision 1.75

1.1       paf         1: /** @file
1.9       paf         2:        Parser: parser @b operators.
1.1       paf         3: 
1.70      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.71      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: 
1.75    ! paf         7:        $Id: op.C,v 1.74 2002/03/18 15:29:45 paf Exp $
1.1       paf         8: */
                      9: 
1.13      paf        10: #include "classes.h"
1.1       paf        11: #include "pa_common.h"
                     12: #include "pa_request.h"
                     13: #include "pa_vint.h"
                     14: #include "pa_sql_connection.h"
                     15: 
1.18      parser     16: // limits
                     17: 
                     18: #define MAX_LOOPS 10000
                     19: 
1.10      paf        20: // defines
                     21: 
                     22: #define OP_CLASS_NAME "OP"
1.11      paf        23: 
1.10      paf        24: // class
                     25: 
                     26: class MOP : public Methoded {
                     27: public:
                     28:        MOP(Pool& pool);
1.11      paf        29: public: // Methoded
1.10      paf        30:        bool used_directly() { return true; }
1.11      paf        31:        void configure_user(Request& r);
1.36      parser     32: 
1.11      paf        33: private:
                     34:        String main_sql_name;
                     35:        String main_sql_drivers_name;
1.10      paf        36: };
                     37: 
                     38: // methods
                     39: 
1.5       paf        40: static void _if(Request& r, const String&, MethodParams *params) {
1.48      parser     41:        Value& condition_code=params->as_junction(0, "condition must be expression");
1.1       paf        42: 
                     43:        bool condition=r.process(condition_code, 
                     44:                0/*no name*/,
                     45:                false/*don't intercept string*/).as_bool();
1.6       paf        46:        if(condition)
1.34      parser     47:                r.write_pass_lang(r.process(params->as_junction(1, "'then' parameter must be code")));
1.50      parser     48:        else if(params->size()>2)
1.34      parser     49:                r.write_pass_lang(r.process(params->as_junction(2, "'else' parameter must be code")));
1.1       paf        50: }
                     51: 
1.5       paf        52: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        53:        Pool& pool=r.pool();
                     54: 
1.60      paf        55:        uchar lang;
1.59      paf        56:        if(params->size()==1)
                     57:                lang=String::UL_AS_IS; // mark as simply 'tainted'. useful in html from sql 
                     58:        else {
                     59:                const String& lang_name=params->as_string(0, "lang must be string");
1.60      paf        60:                lang=untaint_lang_name2enum->get_int(lang_name);
1.59      paf        61:                if(!lang)
                     62:                        throw Exception(0, 0,
                     63:                                &lang_name,
                     64:                                "invalid taint language");
                     65:        }
1.1       paf        66: 
                     67:        {
1.59      paf        68:                Value& vbody=params->as_junction(params->size()-1, "body must be code");
1.1       paf        69:                
                     70:                Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.5       paf        71:                r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1       paf        72:        }
                     73: }
                     74: 
1.5       paf        75: static void _taint(Request& r, const String&, MethodParams *params) {
1.1       paf        76:        Pool& pool=r.pool();
                     77: 
1.60      paf        78:        uchar lang;
1.1       paf        79:        if(params->size()==1)
                     80:                lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
                     81:        else {
1.48      parser     82:                const String& lang_name=params->as_string(0, "lang must be string");
1.60      paf        83:                lang=untaint_lang_name2enum->get_int(lang_name);
1.1       paf        84:                if(!lang)
1.53      parser     85:                        throw Exception(0, 0,
1.3       paf        86:                                &lang_name,
                     87:                                "invalid taint language");
1.1       paf        88:        }
                     89: 
                     90:        {
1.34      parser     91:                Value& vbody=params->as_no_junction(params->size()-1, "body must not be code");
1.1       paf        92:                
                     93:                String result(r.pool());
                     94:                result.append(
                     95:                        vbody.as_string(),  // process marking tainted with that lang
                     96:                        lang, true);  // force result language to specified
                     97:                r.write_pass_lang(result);
                     98:        }
                     99: }
                    100: 
1.5       paf       101: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       102:        // calculate pseudo file name of processed chars
                    103:        // would be something like "/some/file(4) process"
                    104:        char place[MAX_STRING];
                    105: #ifndef NO_STRING_ORIGIN
                    106:        const Origin& origin=method_name.origin();
                    107:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                    108:                origin.file, 1+origin.line,
                    109:                method_name.cstr());
                    110: #else
1.39      parser    111:        strncpy(place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
1.1       paf       112: #endif 
                    113: 
                    114:        VStateless_class& self_class=*r.self->get_class();
1.73      paf       115:        const Method *main_method;
1.1       paf       116:        {
1.73      paf       117:                // temporary remove language change
                    118:                Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.1       paf       119:                // temporary zero @main so to maybe-replace it in processed code
                    120:                Temp_method temp_method_main(self_class, *main_method_name, 0);
                    121:                // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
                    122:                Temp_method temp_method_auto(self_class, *auto_method_name, 0);
                    123:                
                    124:                // evaluate source to process
                    125:                const String& source=
1.61      paf       126:                        r.process(params->as_junction(0, "body must be code")).as_string();
1.1       paf       127: 
                    128:                // process source code, append processed methods to 'self' class
                    129:                // maybe-define new @main
1.67      paf       130:                r.use_buf(
                    131:                        source.cstr(String::UL_UNSPECIFIED, r.connection(0)), 
                    132:                        place, 
                    133:                        &self_class);
1.1       paf       134:                
1.73      paf       135:                // main_method
                    136:                main_method=self_class.get_method(*main_method_name);
                    137:        }
                    138:        // after restoring current-request-lang
                    139:        // maybe-execute @main[]
                    140:        if(main_method) {
                    141:                // execute!     
                    142:                r.execute(*main_method->parser_code);
1.1       paf       143:        }
                    144: }
                    145:        
1.5       paf       146: static void _rem(Request& r, const String&, MethodParams *params) {
1.34      parser    147:        params->as_junction(0, "body must be code");
1.1       paf       148: }
                    149: 
1.5       paf       150: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       151:        Pool& pool=r.pool();
                    152: 
1.34      parser    153:        Value& vcondition=params->as_junction(0, "condition must be expression");
                    154:        Value& body=params->as_junction(1, "body must be code");
1.1       paf       155: 
                    156:        // while...
                    157:        int endless_loop_count=0;
                    158:        while(true) {
1.18      parser    159:                if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.53      parser    160:                        throw Exception(0, 0,
1.1       paf       161:                                &method_name,
                    162:                                "endless loop detected");
                    163: 
                    164:                bool condition=
                    165:                        r.process(
                    166:                                vcondition, 
                    167:                                0/*no name*/,
                    168:                                false/*don't intercept string*/).as_bool();
                    169:                if(!condition) // ...condition is true
                    170:                        break;
                    171: 
                    172:                // write processed body
                    173:                r.write_pass_lang(r.process(body));
                    174:        }
                    175: }
                    176: 
1.5       paf       177: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34      parser    178:        Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37      parser    179:        r.use_file(vfile.as_string());
1.1       paf       180: }
                    181: 
1.5       paf       182: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       183:        Pool& pool=r.pool();
1.48      parser    184:        const String& var_name=params->as_string(0, "var name must be string");
                    185:        int from=params->as_int(1, "from must be int", r);
                    186:        int to=params->as_int(2, "to must be int", r);
1.34      parser    187:        Value& body_code=params->as_junction(3, "body must be code");
1.50      parser    188:        Value *delim_maybe_code=params->size()>4?&params->get(4):0;
1.1       paf       189: 
1.57      paf       190:        if(to-from>=MAX_LOOPS) // too long loop?
                    191:                throw Exception(0, 0,
                    192:                        &method_name,
                    193:                        "endless loop detected");
                    194: 
1.1       paf       195:        bool need_delim=false;
                    196:        VInt *vint=new(pool) VInt(pool, 0);
                    197:        for(int i=from; i<=to; i++) {
                    198:                vint->set_int(i);
1.56      paf       199:                r.root->put_element(var_name, vint);
1.1       paf       200: 
                    201:                Value& processed_body=r.process(body_code);
1.49      parser    202:                if(delim_maybe_code) { // delimiter set?
1.1       paf       203:                        const String *string=processed_body.get_string();
                    204:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
1.49      parser    205:                                r.write_pass_lang(r.process(*delim_maybe_code));
1.1       paf       206:                        need_delim=true;
                    207:                }
                    208:                r.write_pass_lang(processed_body);
                    209:        }
                    210: }
                    211: 
1.15      paf       212: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34      parser    213:        Value& expr=params->as_junction(0, "need expression");
1.1       paf       214:        // evaluate expresion
                    215:        Value *result=r.process(expr, 
1.16      paf       216:                0/*no name YET*/,
1.1       paf       217:                true/*don't intercept string*/).as_expr_result();
1.50      parser    218:        if(params->size()>1) {
1.34      parser    219:                Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1       paf       220: 
                    221:                Pool& pool=r.pool();
                    222:                String& string=*new(pool) String(pool);
                    223:                string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
                    224:                result=new(pool) VString(string);
                    225:        }
1.16      paf       226:        result->set_name(method_name);
1.1       paf       227:        r.write_no_lang(*result);
                    228: }
                    229: 
1.42      parser    230: static void _connect(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       231:        Pool& pool=r.pool();
1.63      paf       232: #ifdef RESOURCES_DEBUG
                    233: struct timeval mt[2];
                    234: #endif
1.34      parser    235:        Value& url=params->as_no_junction(0, "url must not be code");
                    236:        Value& body_code=params->as_junction(1, "body must be code");
1.1       paf       237: 
1.19      parser    238:        Table *protocol2driver_and_client=
1.35      parser    239:                static_cast<Table *>(r.classes_conf.get(r.OP.name()));
1.11      paf       240: 
1.63      paf       241: #ifdef RESOURCES_DEBUG
                    242: //measure:before
                    243: gettimeofday(&mt[0],NULL);
                    244: #endif
1.1       paf       245:        // connect
1.67      paf       246:        SQL_Connection_ptr connection=SQL_driver_manager->get_connection(
1.42      parser    247:                url.as_string(), method_name, protocol2driver_and_client);
1.1       paf       248: 
1.63      paf       249: #ifdef RESOURCES_DEBUG
                    250: //measure:after connect
                    251: gettimeofday(&mt[1],NULL);
                    252: 
                    253: double t[2];
                    254: for(int i=0;i<2;i++)
                    255:     t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                    256: 
                    257: r.sql_connect_time+=t[1]-t[0];
                    258: #endif
1.67      paf       259:        Temp_connection temp_connection(r, connection.get());
1.1       paf       260:        // execute body
1.53      parser    261:        try {
1.67      paf       262:                r.write_assign_lang(r.process(body_code));
1.75    ! paf       263:        } catch(...) { // process problem
1.67      paf       264:                connection->mark_to_rollback();
                    265:                /*re*/throw; 
1.1       paf       266:        }
                    267: }
                    268: 
1.41      parser    269: #ifndef DOXYGEN
1.28      parser    270: struct Switch_data {
                    271:        Value *searching;
                    272:        Value *found;
                    273:        Value *_default;
                    274: };
1.41      parser    275: #endif
1.28      parser    276: static void _switch(Request& r, const String&, MethodParams *params) {
                    277:        void *backup=r.classes_conf.get(*switch_data_name);
                    278:        Switch_data data={&r.process(params->get(0))};  
                    279:        r.classes_conf.put(*switch_data_name, &data);
                    280: 
1.34      parser    281:        r.process(params->as_junction(1, "switch cases must be code")); // and ignore result
1.28      parser    282: 
                    283:        r.classes_conf.put(*switch_data_name, backup);
                    284: 
                    285:        if(Value *code=data.found ? data.found : data._default)
                    286:                r.write_pass_lang(r.process(*code));
                    287: }
                    288: 
1.38      parser    289: static void _case(Request& r, const String& method_name, MethodParams *params) {
                    290:        Pool& pool=r.pool();
                    291: 
                    292:        Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
                    293:        if(!data)
1.53      parser    294:                throw Exception(0, 0,
1.38      parser    295:                        &method_name,
                    296:                        "without switch");
1.28      parser    297: 
                    298:        int count=params->size();
1.34      parser    299:        Value *code=&params->as_junction(--count, "case result must be code");
1.28      parser    300:        for(int i=0; i<count; i++) {
                    301:                Value& value=r.process(params->get(i));
                    302: 
1.36      parser    303:                if(value.as_string() == *case_default_value) {
1.38      parser    304:                        data->_default=code;
1.28      parser    305:                        break;
                    306:                }
                    307: 
                    308:                bool matches;
1.38      parser    309:                if(data->searching->is_string())
                    310:                        matches=data->searching->as_string() == value.as_string();
1.28      parser    311:                else
1.38      parser    312:                        matches=data->searching->as_double() == value.as_double();
1.28      parser    313: 
                    314:                if(matches) {
1.38      parser    315:                        data->found=code;
1.28      parser    316:                        break;
                    317:                }
                    318:        }
                    319: }
                    320: 
1.63      paf       321: // cache--
                    322: 
                    323: // consts
                    324: 
                    325: const int DATA_STRING_SERIALIZED_VERSION=0x0001;
                    326: 
                    327: // helper types
                    328: 
                    329: #ifndef DOXYGEN
                    330: struct Data_string_serialized_prolog {
                    331:        int version;
                    332: };
                    333: #endif
                    334: 
1.68      paf       335: void cache_delete(const String& file_spec) {
                    336:        file_delete(file_spec, false/*fail_on_read_problem*/);
1.63      paf       337: }
1.69      paf       338: 
                    339: #ifndef DOXYGEN
                    340: struct Locked_process_and_cache_put_action_info {
                    341:        Request *r;
                    342:        Value *body;
                    343: };
                    344: #endif
                    345: static void locked_process_and_cache_put_action(int f, void *context) {
                    346:        Locked_process_and_cache_put_action_info& info=
                    347:                *static_cast<Locked_process_and_cache_put_action_info *>(context);
                    348:        
                    349:        // body->process 
                    350:        info.body=&info.r->process(*info.body);
                    351: 
                    352:        // result->string
                    353:        const String& data_string=info.body->as_string();
                    354: 
                    355:        // string -serialize> buffer
1.63      paf       356:        void *data; size_t data_size;
                    357:        data_string.serialize(
                    358:                sizeof(Data_string_serialized_prolog), 
                    359:                data, data_size);
                    360:        Data_string_serialized_prolog& prolog=
                    361:                *static_cast<Data_string_serialized_prolog *>(data);
                    362:        prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.69      paf       363:        
                    364:        // buffer -write> file
                    365:        write(f, data, data_size);
                    366: }
                    367: Value *locked_process_and_cache_put(Request& r, 
                    368:                                                                        Value& body_code,
                    369:                                                                        const String& file_spec) {
                    370:        Locked_process_and_cache_put_action_info info={
                    371:                &r,
                    372:                &body_code,
                    373:        };
                    374: 
                    375:        return file_write_action_under_lock(
                    376:                file_spec, 
                    377:                "cache_put", locked_process_and_cache_put_action, &info,
                    378:                false/*as_text*/,
                    379:                false/*do_append*/,
                    380:                false/*block*/) ? info.body : 0;
1.63      paf       381: }
                    382: String *cache_get(Pool& pool, const String& file_spec) {
                    383:        void* data; size_t data_size;
1.72      paf       384:        if(file_read(pool, file_spec, 
1.63      paf       385:                           data, data_size, 
                    386:                           false/*as_text*/, 
1.69      paf       387:                           false/*fail_on_read_problem*/)
1.72      paf       388:            && data_size/* ignore reads which are empty due to 
                    389:                        non-unary open+lockEX conflict with lockSH */) {
1.63      paf       390:        
1.72      paf       391:                Data_string_serialized_prolog& prolog=
                    392:                        *static_cast<Data_string_serialized_prolog *>(data);
1.63      paf       393: 
1.72      paf       394:                String *result=new(pool) String(pool);
                    395:                if(
                    396:                        data_size>=sizeof(Data_string_serialized_prolog)
                    397:                        && prolog.version==DATA_STRING_SERIALIZED_VERSION
                    398:                        && result->deserialize(
                    399:                                sizeof(Data_string_serialized_prolog),  data, data_size, file_spec.cstr()))
                    400:                        return result;
                    401:        }
1.63      paf       402: 
1.72      paf       403:        return 0;
1.63      paf       404: }
                    405: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                    406:        Pool& pool=r.pool();
                    407:        
                    408:        // file_spec, expires, body code
1.65      paf       409:        const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
                    410:        if(params->size()==1) { // delete
1.68      paf       411:                cache_delete(file_spec);
1.65      paf       412:                return;
                    413:        }
                    414: 
1.63      paf       415:        time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
                    416:        Value& body_code=params->as_junction(2, "body must be code");
                    417: 
                    418:        if(lifespan) { // 'lifespan' specified? try cached copy...
                    419:                size_t size;
                    420:                time_t atime, mtime, ctime;
1.69      paf       421: 
                    422:                // hence we don't hope to have unary create/lockEX
                    423:                // we need some plan to live in a life like that, so... 
                    424:                // worst races plan:
                    425:                // A        B
                    426:                // open
                    427:                //          |open
                    428:                // lockSH
                    429:                //          |nonblocking-lockEX fails
                    430:                // unlockSH
                    431:                // close, cache_get returns 0
                    432:                // open
                    433:                // nonblocking-lockEX succeeds; process, write, close
                    434:                //          |retry1: open
                    435:                // ...
                    436:                //          |lockSH succeeds; ...
                    437: 
1.63      paf       438:                // {file_spec} modification time
1.69      paf       439:                for(int retry=0; retry<2; retry++) {
                    440:                        if(file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/)) // exists?
                    441:                                if(time(0)-mtime > lifespan) // expired
                    442:                                        cache_delete(file_spec);
                    443:                                else // not expired
                    444:                                        if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy?
                    445:                                                // write it out 
                    446:                                                r.write_assign_lang(*cached_body);
                    447:                                                // happy with it
                    448:                                                return;
                    449:                                        }
                    450: 
                    451:                        // non-blocked lock; process; cache it
                    452:                        if(Value *processed_body=locked_process_and_cache_put(r, body_code, file_spec)) {
1.63      paf       453:                                // write it out 
1.69      paf       454:                                r.write_assign_lang(*processed_body);
1.63      paf       455:                                // happy with it
                    456:                                return;
1.69      paf       457:                        } else { // somebody writing result right now
                    458:                                pa_sleep(0, 500000); // waiting half a second
                    459:                                retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63      paf       460:                        }
1.69      paf       461:                }
                    462:                throw Exception(0, 0,
                    463:                        &file_spec,
                    464:                        "locking problem");
                    465:        } else { 
                    466:                // 'lifespan'=0, forget cached copy
1.68      paf       467:                cache_delete(file_spec);
1.69      paf       468:                // process
                    469:                Value& processed_body=r.process(body_code);
                    470:                // write it out 
                    471:                r.write_assign_lang(processed_body);
                    472:                // happy with it
                    473:                return;
                    474:        }
                    475:        // never reached
1.63      paf       476: }
                    477: 
1.74      paf       478: // also used in pa_request.C to pass param to @unhandled_exception
                    479: VHash& exception2vhash(Pool& pool, const Exception& e) {
                    480:        VHash& result=*new(pool) VHash(pool);
                    481:        Hash& hash=result.hash(0);
                    482:        if(const String *type=e.type())
                    483:                hash.put(*exception_type_part_name, new(pool) VString(*type));
                    484:        if(const String *source=e.problem_source()) {
                    485:                result.set_name(*source);
                    486: 
                    487:                hash.put(*exception_source_part_name, new(pool) VString(*source));
                    488: #ifndef NO_STRING_ORIGIN
                    489:                const Origin& origin=source->origin();
                    490:                hash.put(*new(pool) String(pool, "file"),                               
                    491:                        new(pool) VString(*new(pool) String(pool, origin.file)));
                    492:                hash.put(*new(pool) String(pool, "lineno"),
                    493:                        new(pool) VInt(pool, 1+origin.line));
                    494: #endif
                    495:        }
                    496:        if(const char *ecomment=e.comment()) {
                    497:                int comment_size=strlen(ecomment);
                    498:                char *pcomment=(char *)pool.malloc(comment_size);
                    499:                memcpy(pcomment, ecomment, comment_size);
                    500:                hash.put(*exception_comment_part_name, 
                    501:                        new(pool) VString(*new(pool) String(pool, pcomment, comment_size)));
                    502:        }
                    503:        hash.put(*exception_handled_part_name, 
                    504:                new(pool) VBool(pool, false));
                    505: 
                    506:        return result;
                    507: }
                    508: 
                    509: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
                    510:        Pool& pool=r.pool();
                    511: 
                    512:        Value& body_code=params->as_junction(0, "body_code must be code");
                    513:        Value& catch_code=params->as_junction(1, "catch_code must be code");
                    514: 
                    515:        Value *result;
                    516: 
                    517:        // taking snapshot of request processing status
                    518:        int stop_index=r.stack.top_index();
                    519:        Value *sself=r.self, *sroot=r.root, *srcontext=r.rcontext;  
                    520:        WContext *swcontext=r.wcontext; 
                    521:        try {
                    522:                result=&r.process(body_code);
                    523:        } catch(const Exception& e) {
                    524:                // restoring request processing status
                    525:                r.stack.top_index(stop_index);
                    526:                r.self=sself; r.root=sroot, r.rcontext=srcontext; r.wcontext=swcontext;
                    527:                
                    528:                VHash& vhash=exception2vhash(pool, e);
                    529: 
                    530:                Junction *junction=catch_code.get_junction();
                    531:                Value *saved_exception_var_value=junction->root->get_element(*exception_var_name);
                    532:                junction->root->put_element(*exception_var_name, &vhash);
                    533:                result=&r.process(catch_code);
                    534:                bool handled=false;
                    535:                if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
                    536:                        handled=value->as_bool();               
                    537:                junction->root->put_element(*exception_var_name, saved_exception_var_value);
                    538: 
                    539:                if(!handled)
                    540:                        throw(e); // rethrow
                    541:        }
                    542:        // write it out 
                    543:        r.write_pass_lang(*result);
                    544: }
                    545: 
                    546: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
                    547:        Pool& pool=r.pool();
                    548: 
                    549:        if(params->size()==1) {
                    550:                Value& param0=params->get(0);
                    551:                if(Hash *hash=param0.get_hash(&method_name)) {
                    552:                        const String *type=0;
                    553:                        if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
                    554:                                type=&value->as_string();
                    555:                        const String *source=0;
                    556:                        if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
                    557:                                source=&value->as_string();
                    558:                        const char *comment=0;
                    559:                        if(Value *value=
                    560:                                static_cast<Value *>(hash->get(*exception_comment_part_name)))
                    561:                                comment=value->as_string().cstr();
                    562: 
                    563:                        throw Exception(type, 0,
                    564:                                source?source:&method_name,
                    565:                                comment);
                    566:                } else
                    567:                        throw Exception(0, 0,
                    568:                                &method_name,
                    569:                                "one-param version has hash param");
                    570:        } else {
                    571:                const String& type=params->as_string(0, "type must be string");
                    572:                const String& source=params->as_string(1, "source must be string");
                    573:                const char *comment=params->as_string(2, "comment must be string").cstr();
                    574:                throw Exception(&type, 0,
                    575:                        &source,
                    576:                        comment);
                    577:        }
                    578: }
                    579:        
1.10      paf       580: // constructor
                    581: 
1.11      paf       582: MOP::MOP(Pool& apool) : Methoded(apool),
                    583:        main_sql_name(apool, MAIN_SQL_NAME),
                    584:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    585: {
1.10      paf       586:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28      parser    587: 
1.1       paf       588:        // ^if(condition){code-when-true}
                    589:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       590:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       591: 
                    592:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59      paf       593:        add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1       paf       594: 
                    595:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       596:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       597: 
                    598:        // ^process[code]
1.10      paf       599:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       600: 
                    601:        // ^rem{code}
1.51      parser    602:        add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1       paf       603: 
                    604:        // ^while(condition){code}
1.10      paf       605:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       606: 
                    607:        // ^use[file]
1.10      paf       608:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       609: 
1.54      paf       610:        // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10      paf       611:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       612: 
                    613:        // ^eval(expr)
                    614:        // ^eval(expr)[format]
1.10      paf       615:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52      parser    616: 
1.1       paf       617:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       618:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    619: 
1.63      paf       620: 
1.65      paf       621:        // ^cache[file_spec] delete cache
1.63      paf       622:        // ^cache[file_spec](time){code} time=0 no cache
1.65      paf       623:        add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63      paf       624:        
1.28      parser    625:        // switch
                    626: 
                    627:        // ^switch[value]{cases}
                    628:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    629: 
                    630:        // ^case[value]{code}
1.51      parser    631:        add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74      paf       632: 
                    633:        // try-catch
                    634: 
                    635:        // ^try{code}{catch code}
                    636:        add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
                    637:        // ^throw[$exception hash]
                    638:        // ^throw[type;source;comment]
                    639:        add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
                    640: 
1.10      paf       641: }
1.11      paf       642: 
                    643: // constructor & configurator
1.1       paf       644: 
1.10      paf       645: Methoded *MOP_create(Pool& pool) {
1.35      parser    646:        return new(pool) MOP(pool);
1.11      paf       647: }
                    648: 
                    649: void MOP::configure_user(Request& r) {
                    650:        Pool& pool=r.pool();
                    651: 
                    652:        // $MAIN:SQL.drivers
                    653:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    654:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    655:                        if(Table *protocol2library=element->get_table())
                    656:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       657: }

E-mail: