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

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

E-mail: