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

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.81    ! paf         7:        $Id: op.C,v 1.80 2002/03/29 11:28:40 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: 
1.81    ! paf        44:        bool condition=r.process_to_value(condition_code, 
1.1       paf        45:                0/*no name*/,
                     46:                false/*don't intercept string*/).as_bool();
1.6       paf        47:        if(condition)
1.81    ! paf        48:                r.write_pass_lang(r.process_to_value(params->as_junction(1, "'then' parameter must be code")));
1.50      parser     49:        else if(params->size()>2)
1.81    ! paf        50:                r.write_pass_lang(r.process_to_value(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.81    ! paf        72:                r.write_pass_lang(r.process_to_value(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.81    ! paf       127:                        r.process_to_string(params->as_junction(0, "body must be code"));
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=
1.81    ! paf       166:                        r.process_to_value(
1.1       paf       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
1.81    ! paf       174:                r.write_pass_lang(r.process_to_value(body));
1.1       paf       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: 
1.81    ! paf       202:                Value& processed_body=r.process_to_value(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.81    ! paf       206:                                r.write_pass_lang(r.process_to_string(*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
1.81    ! paf       216:        Value *result=r.process_to_value(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.81    ! paf       263:                r.write_assign_lang(r.process_to_value(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) {
1.81    ! paf       278:        Switch_data data={&r.process_to_value(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.81    ! paf       281:        r.process_to_string(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)
1.81    ! paf       284:                r.write_pass_lang(r.process_to_value(*code));
1.28      parser    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++) {
1.81    ! paf       299:                Value& value=r.process_to_value(params->get(i));
1.28      parser    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;
1.79      paf       344:        Cache_data *data;
1.81    ! paf       345:        Value *body_code; const String *evaluated_body;
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 
1.81    ! paf       353:        info.evaluated_body=&info.r->process_to_string(*info.body_code);
1.69      paf       354: 
1.79      paf       355:        // expiration time not spoiled by ^cache(0) or something?
                    356:        if(info.data->expires > time(0)) {
                    357:                // string -serialize> buffer
                    358:                void *data; size_t data_size;
1.81    ! paf       359:                info.evaluated_body->serialize(
1.79      paf       360:                        sizeof(Data_string_serialized_prolog), 
                    361:                        data, data_size);
                    362:                Data_string_serialized_prolog& prolog=
                    363:                        *static_cast<Data_string_serialized_prolog *>(data);
                    364:                prolog.version=DATA_STRING_SERIALIZED_VERSION;
                    365:                prolog.expires=info.data->expires;
                    366:                
                    367:                // buffer -write> file
                    368:                write(f, data, data_size);
                    369:        } else // expired!
                    370:                info.data->expires=0; // flag it so that could be easily checked by caller
1.69      paf       371: }
1.81    ! paf       372: const String *locked_process_and_cache_put(Request& r, 
1.69      paf       373:                                                                        Value& body_code,
1.79      paf       374:                                                                        Cache_data& data,
1.69      paf       375:                                                                        const String& file_spec) {
                    376:        Locked_process_and_cache_put_action_info info={
                    377:                &r,
1.81    ! paf       378:                &data,
        !           379:                &body_code
1.69      paf       380:        };
                    381: 
1.81    ! paf       382:        const String *result=file_write_action_under_lock(
1.69      paf       383:                file_spec, 
                    384:                "cache_put", locked_process_and_cache_put_action, &info,
                    385:                false/*as_text*/,
                    386:                false/*do_append*/,
1.81    ! paf       387:                false/*block*/) ? info.evaluated_body: 0;
1.79      paf       388:        if(data.expires==0)
                    389:                cache_delete(file_spec);
                    390:        return result;
1.63      paf       391: }
1.79      paf       392: String *cache_get(Pool& pool, const String& file_spec, time_t now) {
1.63      paf       393:        void* data; size_t data_size;
1.72      paf       394:        if(file_read(pool, file_spec, 
1.63      paf       395:                           data, data_size, 
                    396:                           false/*as_text*/, 
1.69      paf       397:                           false/*fail_on_read_problem*/)
1.72      paf       398:            && data_size/* ignore reads which are empty due to 
                    399:                        non-unary open+lockEX conflict with lockSH */) {
1.63      paf       400:        
1.72      paf       401:                Data_string_serialized_prolog& prolog=
                    402:                        *static_cast<Data_string_serialized_prolog *>(data);
1.63      paf       403: 
1.72      paf       404:                String *result=new(pool) String(pool);
                    405:                if(
                    406:                        data_size>=sizeof(Data_string_serialized_prolog)
                    407:                        && prolog.version==DATA_STRING_SERIALIZED_VERSION
1.79      paf       408:                        && prolog.expires > now
1.72      paf       409:                        && result->deserialize(
                    410:                                sizeof(Data_string_serialized_prolog),  data, data_size, file_spec.cstr()))
                    411:                        return result;
                    412:        }
1.63      paf       413: 
1.72      paf       414:        return 0;
1.63      paf       415: }
1.79      paf       416: static time_t as_expires(Request& r, const String& method_name, MethodParams *params, 
                    417:                                                int index, time_t now) {
                    418:        time_t result;
                    419:        Value& vlifespan_or_expires=params->get(index);
                    420:        if(strcmp(vlifespan_or_expires.type(), VDATE_TYPE)==0)
                    421:                result=static_cast<VDate&>(vlifespan_or_expires).get_time();
                    422:        else
                    423:                result=now+(time_t)params->as_double(index, "lifespan must be date or number", r);
                    424:        
                    425:        return result;
                    426: }
                    427: static const String as_file_spec(Request&r, MethodParams *params, int index) {
                    428:        return r.absolute(params->as_string(index, "filespec must be string"));
                    429: }
1.63      paf       430: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                    431:        Pool& pool=r.pool();
1.79      paf       432:        time_t now=time(0);
                    433: 
                    434:        // ^cache[filename] ^cache(seconds) ^cache[expires date]
                    435:        if(params->size()==1) {
                    436:                if(params->get(0).is_string()) { // filename?
                    437:                        cache_delete(as_file_spec(r, params, 0));
                    438:                        return;
                    439:                }
                    440: 
                    441:                // secods|expires date
                    442:                Cache_data *data=static_cast<Cache_data *>(r.classes_conf.get(*cache_data_name));
                    443:                if(!data)
                    444:                        throw Exception("parser.runtime",
                    445:                                &method_name,
                    446:                                "expire-time reducing instruction without cache");
                    447:                
                    448:                time_t expires=as_expires(r, method_name, params, 0, now);
                    449:                if(expires < data->expires)
                    450:                        data->expires=expires;
                    451: 
                    452:                return;
                    453:        }
1.63      paf       454:        
                    455:        // file_spec, expires, body code
1.65      paf       456:        const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
                    457: 
1.79      paf       458:        Cache_data data;
                    459:        Temp_hash_value cache_data_setter(r.classes_conf, *cache_data_name, &data);
                    460:        data.expires=as_expires(r, method_name, params, 1, now);
1.63      paf       461:        Value& body_code=params->as_junction(2, "body must be code");
                    462: 
1.79      paf       463:        if(data.expires>now) { // valid 'expires' specified? try cached copy...
1.69      paf       464:                // hence we don't hope to have unary create/lockEX
                    465:                // we need some plan to live in a life like that, so... 
                    466:                // worst races plan:
                    467:                // A        B
                    468:                // open
                    469:                //          |open
                    470:                // lockSH
                    471:                //          |nonblocking-lockEX fails
                    472:                // unlockSH
                    473:                // close, cache_get returns 0
                    474:                // open
                    475:                // nonblocking-lockEX succeeds; process, write, close
                    476:                //          |retry1: open
                    477:                // ...
                    478:                //          |lockSH succeeds; ...
                    479: 
                    480:                for(int retry=0; retry<2; retry++) {
1.79      paf       481:                        if(String *cached_body=cache_get(pool, file_spec, now)) { // have cached copy?
                    482:                                // write it out 
                    483:                                r.write_assign_lang(*cached_body);
                    484:                                // happy with it
                    485:                                return;
                    486:                        }
1.69      paf       487: 
                    488:                        // non-blocked lock; process; cache it
1.81    ! paf       489:                        if(const String*processed_body=
        !           490:                                locked_process_and_cache_put(r, body_code, data, file_spec)) {
1.63      paf       491:                                // write it out 
1.69      paf       492:                                r.write_assign_lang(*processed_body);
1.63      paf       493:                                // happy with it
                    494:                                return;
1.69      paf       495:                        } else { // somebody writing result right now
                    496:                                pa_sleep(0, 500000); // waiting half a second
                    497:                                retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63      paf       498:                        }
1.69      paf       499:                }
1.78      paf       500:                throw Exception(0,
1.69      paf       501:                        &file_spec,
                    502:                        "locking problem");
                    503:        } else { 
1.79      paf       504:                // instructed not to cache; forget cached copy
1.68      paf       505:                cache_delete(file_spec);
1.69      paf       506:                // process
1.81    ! paf       507:                const String& processed_body=r.process_to_string(body_code);
1.69      paf       508:                // write it out 
                    509:                r.write_assign_lang(processed_body);
                    510:                // happy with it
                    511:                return;
                    512:        }
                    513:        // never reached
1.63      paf       514: }
                    515: 
1.74      paf       516: // also used in pa_request.C to pass param to @unhandled_exception
                    517: VHash& exception2vhash(Pool& pool, const Exception& e) {
                    518:        VHash& result=*new(pool) VHash(pool);
                    519:        Hash& hash=result.hash(0);
1.78      paf       520:        if(const char *type=e.type())
                    521:                hash.put(*exception_type_part_name, new(pool) VString(*new(pool) String(pool, type)));
1.76      paf       522:        if(const String *asource=e.problem_source()) {
                    523:                String& source=*new(pool) String(pool); 
                    524:                source.append(*asource, String::UL_TAINTED, true/*forced*/);
                    525:                result.set_name(source);
1.74      paf       526: 
1.76      paf       527:                hash.put(*exception_source_part_name, new(pool) VString(source));
1.74      paf       528: #ifndef NO_STRING_ORIGIN
1.76      paf       529:                const Origin& origin=source.origin();
1.74      paf       530:                hash.put(*new(pool) String(pool, "file"),                               
                    531:                        new(pool) VString(*new(pool) String(pool, origin.file)));
                    532:                hash.put(*new(pool) String(pool, "lineno"),
                    533:                        new(pool) VInt(pool, 1+origin.line));
                    534: #endif
                    535:        }
                    536:        if(const char *ecomment=e.comment()) {
                    537:                int comment_size=strlen(ecomment);
                    538:                char *pcomment=(char *)pool.malloc(comment_size);
                    539:                memcpy(pcomment, ecomment, comment_size);
                    540:                hash.put(*exception_comment_part_name, 
1.76      paf       541:                        new(pool) VString(*new(pool) String(pool, pcomment, comment_size, true/*tainted*/)));
1.74      paf       542:        }
                    543:        hash.put(*exception_handled_part_name, 
                    544:                new(pool) VBool(pool, false));
                    545: 
                    546:        return result;
                    547: }
                    548: 
                    549: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
                    550:        Pool& pool=r.pool();
                    551: 
                    552:        Value& body_code=params->as_junction(0, "body_code must be code");
                    553:        Value& catch_code=params->as_junction(1, "catch_code must be code");
                    554: 
                    555:        Value *result;
                    556: 
                    557:        // taking snapshot of request processing status
1.77      paf       558:        //int ssexception_trace=r.exception_trace.top_index();
                    559:        int sstack=r.stack.top_index();
1.74      paf       560:        Value *sself=r.self, *sroot=r.root, *srcontext=r.rcontext;  
                    561:        WContext *swcontext=r.wcontext; 
                    562:        try {
1.81    ! paf       563:                result=&r.process_to_value(body_code);
1.74      paf       564:        } catch(const Exception& e) {
                    565:                // restoring request processing status
1.77      paf       566:                //r.exception_trace.top_index(ssexception_trace);
                    567:                r.stack.top_index(sstack);
1.74      paf       568:                r.self=sself; r.root=sroot, r.rcontext=srcontext; r.wcontext=swcontext;
1.77      paf       569:                
1.74      paf       570:                
                    571:                VHash& vhash=exception2vhash(pool, e);
                    572: 
                    573:                Junction *junction=catch_code.get_junction();
                    574:                Value *saved_exception_var_value=junction->root->get_element(*exception_var_name);
                    575:                junction->root->put_element(*exception_var_name, &vhash);
1.81    ! paf       576:                result=&r.process_to_value(catch_code);
1.74      paf       577:                bool handled=false;
                    578:                if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
                    579:                        handled=value->as_bool();               
                    580:                junction->root->put_element(*exception_var_name, saved_exception_var_value);
                    581: 
                    582:                if(!handled)
                    583:                        throw(e); // rethrow
                    584:        }
                    585:        // write it out 
                    586:        r.write_pass_lang(*result);
                    587: }
                    588: 
                    589: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
                    590:        Pool& pool=r.pool();
                    591: 
                    592:        if(params->size()==1) {
                    593:                Value& param0=params->get(0);
                    594:                if(Hash *hash=param0.get_hash(&method_name)) {
1.78      paf       595:                        const char *type=0;
1.74      paf       596:                        if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
1.78      paf       597:                                type=value->as_string().cstr();
1.74      paf       598:                        const String *source=0;
                    599:                        if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
                    600:                                source=&value->as_string();
                    601:                        const char *comment=0;
                    602:                        if(Value *value=
                    603:                                static_cast<Value *>(hash->get(*exception_comment_part_name)))
                    604:                                comment=value->as_string().cstr();
                    605: 
1.78      paf       606:                        throw Exception(type,
1.74      paf       607:                                source?source:&method_name,
                    608:                                comment);
                    609:                } else
1.78      paf       610:                        throw Exception("parser.runtime",
1.74      paf       611:                                &method_name,
                    612:                                "one-param version has hash param");
                    613:        } else {
1.78      paf       614:                const char *type=params->as_string(0, "type must be string").cstr();
1.74      paf       615:                const String& source=params->as_string(1, "source must be string");
                    616:                const char *comment=params->as_string(2, "comment must be string").cstr();
1.78      paf       617:                throw Exception(type, &source, comment);
1.74      paf       618:        }
                    619: }
                    620:        
1.10      paf       621: // constructor
                    622: 
1.11      paf       623: MOP::MOP(Pool& apool) : Methoded(apool),
                    624:        main_sql_name(apool, MAIN_SQL_NAME),
                    625:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    626: {
1.10      paf       627:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28      parser    628: 
1.1       paf       629:        // ^if(condition){code-when-true}
                    630:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       631:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       632: 
                    633:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59      paf       634:        add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1       paf       635: 
                    636:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       637:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       638: 
                    639:        // ^process[code]
1.10      paf       640:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       641: 
                    642:        // ^rem{code}
1.51      parser    643:        add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1       paf       644: 
                    645:        // ^while(condition){code}
1.10      paf       646:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       647: 
                    648:        // ^use[file]
1.10      paf       649:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       650: 
1.54      paf       651:        // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10      paf       652:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       653: 
                    654:        // ^eval(expr)
                    655:        // ^eval(expr)[format]
1.10      paf       656:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52      parser    657: 
1.1       paf       658:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       659:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    660: 
1.63      paf       661: 
1.65      paf       662:        // ^cache[file_spec] delete cache
1.63      paf       663:        // ^cache[file_spec](time){code} time=0 no cache
1.65      paf       664:        add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63      paf       665:        
1.28      parser    666:        // switch
                    667: 
                    668:        // ^switch[value]{cases}
                    669:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    670: 
                    671:        // ^case[value]{code}
1.51      parser    672:        add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74      paf       673: 
                    674:        // try-catch
                    675: 
                    676:        // ^try{code}{catch code}
                    677:        add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
                    678:        // ^throw[$exception hash]
                    679:        // ^throw[type;source;comment]
                    680:        add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
                    681: 
1.10      paf       682: }
1.11      paf       683: 
                    684: // constructor & configurator
1.1       paf       685: 
1.10      paf       686: Methoded *MOP_create(Pool& pool) {
1.35      parser    687:        return new(pool) MOP(pool);
1.11      paf       688: }
                    689: 
                    690: void MOP::configure_user(Request& r) {
                    691:        Pool& pool=r.pool();
                    692: 
                    693:        // $MAIN:SQL.drivers
                    694:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    695:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    696:                        if(Table *protocol2library=element->get_table())
                    697:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       698: }

E-mail: