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

1.1       paf         1: /** @file
1.9       paf         2:        Parser: parser @b operators.
1.1       paf         3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: */
1.31    ! parser      8: static const char *RCSId="$Id: op.C,v 1.30 2001/06/28 07:44:17 parser Exp $"; 
1.1       paf         9: 
1.13      paf        10: #include "classes.h"
1.1       paf        11: #include "pa_config_includes.h"
                     12: #include "pa_common.h"
                     13: #include "pa_request.h"
                     14: #include "pa_vint.h"
                     15: #include "pa_sql_connection.h"
                     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"
                     24: 
1.11      paf        25: #define MAIN_SQL_NAME "SQL"
                     26: #define MAIN_SQL_DRIVERS_NAME "drivers"
                     27: 
1.28      parser     28: #define SWITCH_DATA_NAME "SWITCH-DATA"
                     29: #define DEFAULT_VALUE "DEFAULT"
                     30: 
1.11      paf        31: // local variable
                     32: 
                     33: static Methoded *OP;
                     34: 
1.10      paf        35: // class
                     36: 
                     37: class MOP : public Methoded {
                     38: public:
                     39:        MOP(Pool& pool);
1.11      paf        40: public: // Methoded
1.10      paf        41:        bool used_directly() { return true; }
1.11      paf        42:        void configure_user(Request& r);
                     43: private:
                     44:        String main_sql_name;
                     45:        String main_sql_drivers_name;
1.10      paf        46: };
                     47: 
                     48: // methods
                     49: 
1.5       paf        50: static void _if(Request& r, const String&, MethodParams *params) {
                     51:        Value& condition_code=params->get(0);
1.1       paf        52: 
                     53:        bool condition=r.process(condition_code, 
                     54:                0/*no name*/,
                     55:                false/*don't intercept string*/).as_bool();
1.6       paf        56:        if(condition)
1.25      parser     57:                r.write_pass_lang(r.process(params->get_junction(1, "'then' parameter must be code")));
1.6       paf        58:        else if(params->size()==3)
1.25      parser     59:                r.write_pass_lang(r.process(params->get_junction(2, "'else' parameter must be code")));
1.1       paf        60: }
                     61: 
1.5       paf        62: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        63:        Pool& pool=r.pool();
                     64: 
1.5       paf        65:        const String& lang_name=r.process(params->get(0)).as_string();
1.1       paf        66:        String::Untaint_lang lang=static_cast<String::Untaint_lang>(
                     67:                untaint_lang_name2enum->get_int(lang_name));
                     68:        if(!lang)
                     69:                PTHROW(0, 0,
                     70:                        &lang_name,
                     71:                        "invalid untaint language");
                     72: 
                     73:        {
1.5       paf        74:                Value& vbody=params->get_junction(1, "body must be code");
1.1       paf        75:                
                     76:                Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.5       paf        77:                r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1       paf        78:        }
                     79: }
                     80: 
1.5       paf        81: static void _taint(Request& r, const String&, MethodParams *params) {
1.1       paf        82:        Pool& pool=r.pool();
                     83: 
                     84:        String::Untaint_lang lang;
                     85:        if(params->size()==1)
                     86:                lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
                     87:        else {
1.3       paf        88:                const String& lang_name=
1.5       paf        89:                        r.process(params->get(0)).as_string();
1.1       paf        90:                lang=static_cast<String::Untaint_lang>(
                     91:                        untaint_lang_name2enum->get_int(lang_name));
                     92:                if(!lang)
                     93:                        PTHROW(0, 0,
1.3       paf        94:                                &lang_name,
                     95:                                "invalid taint language");
1.1       paf        96:        }
                     97: 
                     98:        {
1.5       paf        99:                Value& vbody=params->get_no_junction(params->size()-1, "body must not be code");
1.1       paf       100:                
                    101:                String result(r.pool());
                    102:                result.append(
                    103:                        vbody.as_string(),  // process marking tainted with that lang
                    104:                        lang, true);  // force result language to specified
                    105:                r.write_pass_lang(result);
                    106:        }
                    107: }
                    108: 
1.5       paf       109: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       110:        // calculate pseudo file name of processed chars
                    111:        // would be something like "/some/file(4) process"
                    112:        char place[MAX_STRING];
                    113: #ifndef NO_STRING_ORIGIN
                    114:        const Origin& origin=method_name.origin();
                    115:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                    116:                origin.file, 1+origin.line,
                    117:                method_name.cstr());
                    118: #else
                    119:        strncpy(place, MAX_STRING, method_name.cstr());
                    120: #endif 
                    121: 
                    122:        VStateless_class& self_class=*r.self->get_class();
                    123:        {
                    124:                // temporary zero @main so to maybe-replace it in processed code
                    125:                Temp_method temp_method_main(self_class, *main_method_name, 0);
                    126:                // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
                    127:                Temp_method temp_method_auto(self_class, *auto_method_name, 0);
                    128:                
                    129:                // evaluate source to process
                    130:                const String& source=
1.5       paf       131:                        r.process(params->get(0)).as_string();
1.1       paf       132: 
                    133:                // process source code, append processed methods to 'self' class
                    134:                // maybe-define new @main
                    135:                r.use_buf(source.cstr(), place, &self_class);
                    136:                
                    137:                // maybe-execute @main[]
                    138:                if(const Method *method=self_class.get_method(*main_method_name)) {
                    139:                        // execute!     
                    140:                        r.execute(*method->parser_code);
                    141:                }
                    142:        }
                    143: }
                    144:        
1.5       paf       145: static void _rem(Request& r, const String&, MethodParams *params) {
                    146:        params->get_junction(0, "body must be code");
1.1       paf       147: }
                    148: 
1.5       paf       149: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       150:        Pool& pool=r.pool();
                    151: 
1.5       paf       152:        Value& vcondition=params->get_junction(0, "condition must be expression");
                    153:        Value& body=params->get_junction(1, "body must be code");
1.1       paf       154: 
                    155:        // while...
                    156:        int endless_loop_count=0;
                    157:        while(true) {
1.18      parser    158:                if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.1       paf       159:                        PTHROW(0, 0,
                    160:                                &method_name,
                    161:                                "endless loop detected");
                    162: 
                    163:                bool condition=
                    164:                        r.process(
                    165:                                vcondition, 
                    166:                                0/*no name*/,
                    167:                                false/*don't intercept string*/).as_bool();
                    168:                if(!condition) // ...condition is true
                    169:                        break;
                    170: 
                    171:                // write processed body
                    172:                r.write_pass_lang(r.process(body));
                    173:        }
                    174: }
                    175: 
1.5       paf       176: static void _use(Request& r, const String& method_name, MethodParams *params) {
                    177:        Value& vfile=params->get_no_junction(0, "file name must not be code");
1.1       paf       178:        r.use_file(r.absolute(vfile.as_string()));
                    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.5       paf       183:        const String& var_name=r.process(params->get(0)).as_string();
1.18      parser    184:        int from=r.process(params->get(1)).as_int();
                    185:        int to=r.process(params->get(2)).as_int();
1.5       paf       186:        Value& body_code=params->get_junction(3, "body must be code");
                    187:        Value *delim_code=params->size()==3+1+1?&params->get(3+1):0;
1.1       paf       188: 
                    189:        bool need_delim=false;
                    190:        VInt *vint=new(pool) VInt(pool, 0);
                    191:        int endless_loop_count=0;
                    192:        for(int i=from; i<=to; i++) {
1.18      parser    193:                if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.1       paf       194:                        PTHROW(0, 0,
                    195:                                &method_name,
                    196:                                "endless loop detected");
                    197:                vint->set_int(i);
1.4       paf       198:                r.root->put_element(var_name, vint);
1.1       paf       199: 
                    200:                Value& processed_body=r.process(body_code);
                    201:                if(delim_code) { // delimiter set?
                    202:                        const String *string=processed_body.get_string();
                    203:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    204:                                r.write_pass_lang(r.process(*delim_code));
                    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.5       paf       212:        Value& expr=params->get_junction(0, "need expression");
1.1       paf       213:        // evaluate expresion
                    214:        Value *result=r.process(expr, 
1.16      paf       215:                0/*no name YET*/,
1.1       paf       216:                true/*don't intercept string*/).as_expr_result();
                    217:        if(params->size()==2) {
1.5       paf       218:                Value& fmt=params->get_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: 
                    229: 
                    230: typedef double (*math_one_double_op_func_ptr)(double);
                    231: static double round(double op) { return floor(op+0.5); }
                    232: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
                    233: 
1.31    ! parser    234: static void math_one_op(Request& r, 
        !           235:                                                const String& method_name, MethodParams *params,
        !           236:                                                math_one_double_op_func_ptr func) {
1.1       paf       237:        Pool& pool=r.pool();
1.5       paf       238:        Value& param=params->get_junction(0, "parameter must be expression");
1.1       paf       239: 
                    240:        Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
1.16      paf       241:        result.set_name(method_name);
1.1       paf       242:        r.write_no_lang(result);
                    243: }
                    244: 
1.31    ! parser    245: #define _MATH(name) \
        !           246:        static void _##name(Request& r, const String& method_name, MethodParams *params) {\
        !           247:                math_one_op(r, method_name, params,     &round);\
        !           248:        }
        !           249: _MATH(round);  _MATH(floor);   _MATH(ceiling);
        !           250: _MATH(abs);    _MATH(sign);
        !           251: _MATH(exp);    _MATH(log);     
        !           252: _MATH(sin);    _MATH(asin);    
        !           253: _MATH(cos);    _MATH(acos);    
        !           254: _MATH(tan);    _MATH(atan);
1.27      parser    255: 
                    256: 
1.5       paf       257: static void _connect(Request& r, const String&, MethodParams *params) {
1.1       paf       258:        Pool& pool=r.pool();
                    259: 
1.5       paf       260:        Value& url=params->get_no_junction(0, "url must not be code");
                    261:        Value& body_code=params->get_junction(1, "body must be code");
1.1       paf       262: 
1.19      parser    263:        Table *protocol2driver_and_client=
                    264:                static_cast<Table *>(r.classes_conf.get(OP->name()));
1.11      paf       265: 
1.1       paf       266:        // connect
                    267:        SQL_Connection& connection=SQL_driver_manager->get_connection(
1.19      parser    268:                url.as_string(), protocol2driver_and_client);
1.1       paf       269: 
                    270:        Exception rethrow_me;
                    271:        // remember/set current connection
                    272:        SQL_Connection *saved_connection=r.connection;
                    273:        r.connection=&connection;
                    274:        // execute body
                    275:        bool body_failed=false;  
                    276:        PTRY
                    277:                r.write_assign_lang(r.process(body_code));
                    278:        PCATCH(e) { // connect/process problem
                    279:                rethrow_me=e;  body_failed=true; 
                    280:        }
                    281:        PEND_CATCH
                    282: 
                    283:        bool finalizer_failed=false;
                    284:        PTRY
                    285:                // FINALLY
                    286:                if(body_failed)
                    287:                        connection.rollback();
                    288:                else
                    289:                        connection.commit();
                    290:        PCATCH(e) { // commit/rollback problem
                    291:                rethrow_me=e;  finalizer_failed=true; 
                    292:        }
                    293:        PEND_CATCH
                    294: 
                    295:        // close connection [cache it]
1.21      parser    296:        connection.close();
1.1       paf       297:        // recall current connection from remembered
                    298:        r.connection=saved_connection;
                    299: 
                    300:        if(body_failed || finalizer_failed) // were there an exception for us to rethrow?
                    301:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    302:                        rethrow_me.problem_source(),
                    303:                        rethrow_me.comment());
                    304: }
                    305: 
1.28      parser    306: static String *switch_data_name;
                    307: static String *default_value;
                    308: 
                    309: struct Switch_data {
                    310:        Value *searching;
                    311:        Value *found;
                    312:        Value *_default;
                    313: };
                    314: 
                    315: static void _switch(Request& r, const String&, MethodParams *params) {
                    316:        void *backup=r.classes_conf.get(*switch_data_name);
                    317:        Switch_data data={&r.process(params->get(0))};  
                    318:        r.classes_conf.put(*switch_data_name, &data);
                    319: 
                    320:        r.process(params->get_junction(1, "switch cases must be code")); // and ignore result
                    321: 
                    322:        r.classes_conf.put(*switch_data_name, backup);
                    323: 
                    324:        if(Value *code=data.found ? data.found : data._default)
                    325:                r.write_pass_lang(r.process(*code));
                    326: }
                    327: 
                    328: static void _case(Request& r, const String&, MethodParams *params) {
                    329:        Switch_data& data=*static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
                    330: 
                    331:        int count=params->size();
                    332:        Value *code=&params->get_junction(--count, "case result must be code");
                    333:        for(int i=0; i<count; i++) {
                    334:                Value& value=r.process(params->get(i));
                    335: 
                    336:                if(value.as_string() == *default_value) {
                    337:                        data._default=code;
                    338:                        break;
                    339:                }
                    340: 
                    341:                bool matches;
                    342:                if(data.searching->is_string())
                    343:                        matches=data.searching->as_string() == value.as_string();
                    344:                else
                    345:                        matches=data.searching->as_double() == value.as_double();
                    346: 
                    347:                if(matches) {
                    348:                        data.found=code;
                    349:                        break;
                    350:                }
                    351:        }
                    352: }
                    353: 
1.10      paf       354: // constructor
                    355: 
1.11      paf       356: MOP::MOP(Pool& apool) : Methoded(apool),
                    357:        main_sql_name(apool, MAIN_SQL_NAME),
                    358:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    359: {
1.10      paf       360:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.1       paf       361: 
1.28      parser    362:        switch_data_name=NEW String(pool(), SWITCH_DATA_NAME);
                    363:        default_value=NEW String(pool(), DEFAULT_VALUE);
                    364: 
1.1       paf       365:        // ^if(condition){code-when-true}
                    366:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       367:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       368: 
                    369:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       370:        add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
1.1       paf       371: 
                    372:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       373:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       374: 
                    375:        // ^process[code]
1.10      paf       376:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       377: 
                    378:        // ^rem{code}
1.26      parser    379:        add_native_method("rem", Method::CT_ANY, _rem, 1, 1000);
1.1       paf       380: 
                    381:        // ^while(condition){code}
1.10      paf       382:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       383: 
                    384:        // ^use[file]
1.10      paf       385:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       386: 
                    387:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
1.10      paf       388:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       389: 
                    390:        // ^eval(expr)
                    391:        // ^eval(expr)[format]
1.10      paf       392:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.1       paf       393: 
                    394: 
                    395:        // math functions
                    396: 
                    397:        // ^round(expr) 
1.31    ! parser    398:        #define ADD_MATH(name) add_native_method(#name, Method::CT_ANY, _##name, 1, 1);
        !           399:        ADD_MATH(round);        ADD_MATH(floor);        ADD_MATH(ceiling);
        !           400:        ADD_MATH(abs);  ADD_MATH(sign);
        !           401:        ADD_MATH(exp);  ADD_MATH(log);  
        !           402:        ADD_MATH(sin);  ADD_MATH(asin); 
        !           403:        ADD_MATH(cos);  ADD_MATH(acos); 
        !           404:        ADD_MATH(tan);  ADD_MATH(atan);
1.1       paf       405: 
                    406:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       407:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    408: 
1.28      parser    409:        // switch
                    410: 
                    411:        // ^switch[value]{cases}
                    412:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    413: 
                    414:        // ^case[value]{code}
                    415:        add_native_method("case", Method::CT_ANY, _case, 2, 1000);
1.10      paf       416: }
1.11      paf       417: 
                    418: // constructor & configurator
1.1       paf       419: 
1.10      paf       420: Methoded *MOP_create(Pool& pool) {
1.11      paf       421:        return OP=new(pool) MOP(pool);
                    422: }
                    423: 
                    424: 
                    425: void MOP::configure_user(Request& r) {
                    426:        Pool& pool=r.pool();
                    427: 
                    428:        // $MAIN:SQL.drivers
                    429:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    430:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    431:                        if(Table *protocol2library=element->get_table())
                    432:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       433: }

E-mail: