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

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.74    ! paf         7:        $Id: op.C,v 1.73 2002/03/04 14:51:10 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));
                    263:                
                    264:        } catch(...) { // process/commit problem
                    265:                connection->mark_to_rollback();
                    266:                /*re*/throw; 
1.1       paf       267:        }
                    268: }
                    269: 
1.41      parser    270: #ifndef DOXYGEN
1.28      parser    271: struct Switch_data {
                    272:        Value *searching;
                    273:        Value *found;
                    274:        Value *_default;
                    275: };
1.41      parser    276: #endif
1.28      parser    277: static void _switch(Request& r, const String&, MethodParams *params) {
                    278:        void *backup=r.classes_conf.get(*switch_data_name);
                    279:        Switch_data data={&r.process(params->get(0))};  
                    280:        r.classes_conf.put(*switch_data_name, &data);
                    281: 
1.34      parser    282:        r.process(params->as_junction(1, "switch cases must be code")); // and ignore result
1.28      parser    283: 
                    284:        r.classes_conf.put(*switch_data_name, backup);
                    285: 
                    286:        if(Value *code=data.found ? data.found : data._default)
                    287:                r.write_pass_lang(r.process(*code));
                    288: }
                    289: 
1.38      parser    290: static void _case(Request& r, const String& method_name, MethodParams *params) {
                    291:        Pool& pool=r.pool();
                    292: 
                    293:        Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
                    294:        if(!data)
1.53      parser    295:                throw Exception(0, 0,
1.38      parser    296:                        &method_name,
                    297:                        "without switch");
1.28      parser    298: 
                    299:        int count=params->size();
1.34      parser    300:        Value *code=&params->as_junction(--count, "case result must be code");
1.28      parser    301:        for(int i=0; i<count; i++) {
                    302:                Value& value=r.process(params->get(i));
                    303: 
1.36      parser    304:                if(value.as_string() == *case_default_value) {
1.38      parser    305:                        data->_default=code;
1.28      parser    306:                        break;
                    307:                }
                    308: 
                    309:                bool matches;
1.38      parser    310:                if(data->searching->is_string())
                    311:                        matches=data->searching->as_string() == value.as_string();
1.28      parser    312:                else
1.38      parser    313:                        matches=data->searching->as_double() == value.as_double();
1.28      parser    314: 
                    315:                if(matches) {
1.38      parser    316:                        data->found=code;
1.28      parser    317:                        break;
                    318:                }
                    319:        }
                    320: }
                    321: 
1.63      paf       322: // cache--
                    323: 
                    324: // consts
                    325: 
                    326: const int DATA_STRING_SERIALIZED_VERSION=0x0001;
                    327: 
                    328: // helper types
                    329: 
                    330: #ifndef DOXYGEN
                    331: struct Data_string_serialized_prolog {
                    332:        int version;
                    333: };
                    334: #endif
                    335: 
1.68      paf       336: void cache_delete(const String& file_spec) {
                    337:        file_delete(file_spec, false/*fail_on_read_problem*/);
1.63      paf       338: }
1.69      paf       339: 
                    340: #ifndef DOXYGEN
                    341: struct Locked_process_and_cache_put_action_info {
                    342:        Request *r;
                    343:        Value *body;
                    344: };
                    345: #endif
                    346: static void locked_process_and_cache_put_action(int f, void *context) {
                    347:        Locked_process_and_cache_put_action_info& info=
                    348:                *static_cast<Locked_process_and_cache_put_action_info *>(context);
                    349:        
                    350:        // body->process 
                    351:        info.body=&info.r->process(*info.body);
                    352: 
                    353:        // result->string
                    354:        const String& data_string=info.body->as_string();
                    355: 
                    356:        // string -serialize> buffer
1.63      paf       357:        void *data; size_t data_size;
                    358:        data_string.serialize(
                    359:                sizeof(Data_string_serialized_prolog), 
                    360:                data, data_size);
                    361:        Data_string_serialized_prolog& prolog=
                    362:                *static_cast<Data_string_serialized_prolog *>(data);
                    363:        prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.69      paf       364:        
                    365:        // buffer -write> file
                    366:        write(f, data, data_size);
                    367: }
                    368: Value *locked_process_and_cache_put(Request& r, 
                    369:                                                                        Value& body_code,
                    370:                                                                        const String& file_spec) {
                    371:        Locked_process_and_cache_put_action_info info={
                    372:                &r,
                    373:                &body_code,
                    374:        };
                    375: 
                    376:        return file_write_action_under_lock(
                    377:                file_spec, 
                    378:                "cache_put", locked_process_and_cache_put_action, &info,
                    379:                false/*as_text*/,
                    380:                false/*do_append*/,
                    381:                false/*block*/) ? info.body : 0;
1.63      paf       382: }
                    383: String *cache_get(Pool& pool, const String& file_spec) {
                    384:        void* data; size_t data_size;
1.72      paf       385:        if(file_read(pool, file_spec, 
1.63      paf       386:                           data, data_size, 
                    387:                           false/*as_text*/, 
1.69      paf       388:                           false/*fail_on_read_problem*/)
1.72      paf       389:            && data_size/* ignore reads which are empty due to 
                    390:                        non-unary open+lockEX conflict with lockSH */) {
1.63      paf       391:        
1.72      paf       392:                Data_string_serialized_prolog& prolog=
                    393:                        *static_cast<Data_string_serialized_prolog *>(data);
1.63      paf       394: 
1.72      paf       395:                String *result=new(pool) String(pool);
                    396:                if(
                    397:                        data_size>=sizeof(Data_string_serialized_prolog)
                    398:                        && prolog.version==DATA_STRING_SERIALIZED_VERSION
                    399:                        && result->deserialize(
                    400:                                sizeof(Data_string_serialized_prolog),  data, data_size, file_spec.cstr()))
                    401:                        return result;
                    402:        }
1.63      paf       403: 
1.72      paf       404:        return 0;
1.63      paf       405: }
                    406: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                    407:        Pool& pool=r.pool();
                    408:        
                    409:        // file_spec, expires, body code
1.65      paf       410:        const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
                    411:        if(params->size()==1) { // delete
1.68      paf       412:                cache_delete(file_spec);
1.65      paf       413:                return;
                    414:        }
                    415: 
1.63      paf       416:        time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
                    417:        Value& body_code=params->as_junction(2, "body must be code");
                    418: 
                    419:        if(lifespan) { // 'lifespan' specified? try cached copy...
                    420:                size_t size;
                    421:                time_t atime, mtime, ctime;
1.69      paf       422: 
                    423:                // hence we don't hope to have unary create/lockEX
                    424:                // we need some plan to live in a life like that, so... 
                    425:                // worst races plan:
                    426:                // A        B
                    427:                // open
                    428:                //          |open
                    429:                // lockSH
                    430:                //          |nonblocking-lockEX fails
                    431:                // unlockSH
                    432:                // close, cache_get returns 0
                    433:                // open
                    434:                // nonblocking-lockEX succeeds; process, write, close
                    435:                //          |retry1: open
                    436:                // ...
                    437:                //          |lockSH succeeds; ...
                    438: 
1.63      paf       439:                // {file_spec} modification time
1.69      paf       440:                for(int retry=0; retry<2; retry++) {
                    441:                        if(file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/)) // exists?
                    442:                                if(time(0)-mtime > lifespan) // expired
                    443:                                        cache_delete(file_spec);
                    444:                                else // not expired
                    445:                                        if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy?
                    446:                                                // write it out 
                    447:                                                r.write_assign_lang(*cached_body);
                    448:                                                // happy with it
                    449:                                                return;
                    450:                                        }
                    451: 
                    452:                        // non-blocked lock; process; cache it
                    453:                        if(Value *processed_body=locked_process_and_cache_put(r, body_code, file_spec)) {
1.63      paf       454:                                // write it out 
1.69      paf       455:                                r.write_assign_lang(*processed_body);
1.63      paf       456:                                // happy with it
                    457:                                return;
1.69      paf       458:                        } else { // somebody writing result right now
                    459:                                pa_sleep(0, 500000); // waiting half a second
                    460:                                retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63      paf       461:                        }
1.69      paf       462:                }
                    463:                throw Exception(0, 0,
                    464:                        &file_spec,
                    465:                        "locking problem");
                    466:        } else { 
                    467:                // 'lifespan'=0, forget cached copy
1.68      paf       468:                cache_delete(file_spec);
1.69      paf       469:                // process
                    470:                Value& processed_body=r.process(body_code);
                    471:                // write it out 
                    472:                r.write_assign_lang(processed_body);
                    473:                // happy with it
                    474:                return;
                    475:        }
                    476:        // never reached
1.63      paf       477: }
                    478: 
1.74    ! paf       479: // also used in pa_request.C to pass param to @unhandled_exception
        !           480: VHash& exception2vhash(Pool& pool, const Exception& e) {
        !           481:        VHash& result=*new(pool) VHash(pool);
        !           482:        Hash& hash=result.hash(0);
        !           483:        if(const String *type=e.type())
        !           484:                hash.put(*exception_type_part_name, new(pool) VString(*type));
        !           485:        if(const String *source=e.problem_source()) {
        !           486:                result.set_name(*source);
        !           487: 
        !           488:                hash.put(*exception_source_part_name, new(pool) VString(*source));
        !           489: #ifndef NO_STRING_ORIGIN
        !           490:                const Origin& origin=source->origin();
        !           491:                hash.put(*new(pool) String(pool, "file"),                               
        !           492:                        new(pool) VString(*new(pool) String(pool, origin.file)));
        !           493:                hash.put(*new(pool) String(pool, "lineno"),
        !           494:                        new(pool) VInt(pool, 1+origin.line));
        !           495: #endif
        !           496:        }
        !           497:        if(const char *ecomment=e.comment()) {
        !           498:                int comment_size=strlen(ecomment);
        !           499:                char *pcomment=(char *)pool.malloc(comment_size);
        !           500:                memcpy(pcomment, ecomment, comment_size);
        !           501:                hash.put(*exception_comment_part_name, 
        !           502:                        new(pool) VString(*new(pool) String(pool, pcomment, comment_size)));
        !           503:        }
        !           504:        hash.put(*exception_handled_part_name, 
        !           505:                new(pool) VBool(pool, false));
        !           506: 
        !           507:        return result;
        !           508: }
        !           509: 
        !           510: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
        !           511:        Pool& pool=r.pool();
        !           512: 
        !           513:        Value& body_code=params->as_junction(0, "body_code must be code");
        !           514:        Value& catch_code=params->as_junction(1, "catch_code must be code");
        !           515: 
        !           516:        Value *result;
        !           517: 
        !           518:        // taking snapshot of request processing status
        !           519:        int stop_index=r.stack.top_index();
        !           520:        Value *sself=r.self, *sroot=r.root, *srcontext=r.rcontext;  
        !           521:        WContext *swcontext=r.wcontext; 
        !           522:        try {
        !           523:                result=&r.process(body_code);
        !           524:        } catch(const Exception& e) {
        !           525:                // restoring request processing status
        !           526:                r.stack.top_index(stop_index);
        !           527:                r.self=sself; r.root=sroot, r.rcontext=srcontext; r.wcontext=swcontext;
        !           528:                
        !           529:                VHash& vhash=exception2vhash(pool, e);
        !           530: 
        !           531:                Junction *junction=catch_code.get_junction();
        !           532:                Value *saved_exception_var_value=junction->root->get_element(*exception_var_name);
        !           533:                junction->root->put_element(*exception_var_name, &vhash);
        !           534:                result=&r.process(catch_code);
        !           535:                bool handled=false;
        !           536:                if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
        !           537:                        handled=value->as_bool();               
        !           538:                junction->root->put_element(*exception_var_name, saved_exception_var_value);
        !           539: 
        !           540:                if(!handled)
        !           541:                        throw(e); // rethrow
        !           542:        }
        !           543:        // write it out 
        !           544:        r.write_pass_lang(*result);
        !           545: }
        !           546: 
        !           547: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
        !           548:        Pool& pool=r.pool();
        !           549: 
        !           550:        if(params->size()==1) {
        !           551:                Value& param0=params->get(0);
        !           552:                if(Hash *hash=param0.get_hash(&method_name)) {
        !           553:                        const String *type=0;
        !           554:                        if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
        !           555:                                type=&value->as_string();
        !           556:                        const String *source=0;
        !           557:                        if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
        !           558:                                source=&value->as_string();
        !           559:                        const char *comment=0;
        !           560:                        if(Value *value=
        !           561:                                static_cast<Value *>(hash->get(*exception_comment_part_name)))
        !           562:                                comment=value->as_string().cstr();
        !           563: 
        !           564:                        throw Exception(type, 0,
        !           565:                                source?source:&method_name,
        !           566:                                comment);
        !           567:                } else
        !           568:                        throw Exception(0, 0,
        !           569:                                &method_name,
        !           570:                                "one-param version has hash param");
        !           571:        } else {
        !           572:                const String& type=params->as_string(0, "type must be string");
        !           573:                const String& source=params->as_string(1, "source must be string");
        !           574:                const char *comment=params->as_string(2, "comment must be string").cstr();
        !           575:                throw Exception(&type, 0,
        !           576:                        &source,
        !           577:                        comment);
        !           578:        }
        !           579: }
        !           580:        
1.10      paf       581: // constructor
                    582: 
1.11      paf       583: MOP::MOP(Pool& apool) : Methoded(apool),
                    584:        main_sql_name(apool, MAIN_SQL_NAME),
                    585:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    586: {
1.10      paf       587:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28      parser    588: 
1.1       paf       589:        // ^if(condition){code-when-true}
                    590:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       591:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       592: 
                    593:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59      paf       594:        add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1       paf       595: 
                    596:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       597:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       598: 
                    599:        // ^process[code]
1.10      paf       600:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       601: 
                    602:        // ^rem{code}
1.51      parser    603:        add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1       paf       604: 
                    605:        // ^while(condition){code}
1.10      paf       606:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       607: 
                    608:        // ^use[file]
1.10      paf       609:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       610: 
1.54      paf       611:        // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10      paf       612:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       613: 
                    614:        // ^eval(expr)
                    615:        // ^eval(expr)[format]
1.10      paf       616:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52      parser    617: 
1.1       paf       618:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       619:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    620: 
1.63      paf       621: 
1.65      paf       622:        // ^cache[file_spec] delete cache
1.63      paf       623:        // ^cache[file_spec](time){code} time=0 no cache
1.65      paf       624:        add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63      paf       625:        
1.28      parser    626:        // switch
                    627: 
                    628:        // ^switch[value]{cases}
                    629:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    630: 
                    631:        // ^case[value]{code}
1.51      parser    632:        add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74    ! paf       633: 
        !           634:        // try-catch
        !           635: 
        !           636:        // ^try{code}{catch code}
        !           637:        add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
        !           638:        // ^throw[$exception hash]
        !           639:        // ^throw[type;source;comment]
        !           640:        add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
        !           641: 
1.10      paf       642: }
1.11      paf       643: 
                    644: // constructor & configurator
1.1       paf       645: 
1.10      paf       646: Methoded *MOP_create(Pool& pool) {
1.35      parser    647:        return new(pool) MOP(pool);
1.11      paf       648: }
                    649: 
                    650: void MOP::configure_user(Request& r) {
                    651:        Pool& pool=r.pool();
                    652: 
                    653:        // $MAIN:SQL.drivers
                    654:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    655:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    656:                        if(Table *protocol2library=element->get_table())
                    657:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       658: }

E-mail: