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

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

E-mail: