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

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

E-mail: