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

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.32    ! parser      8: static const char *RCSId="$Id: op.C,v 1.31 2001/07/03 09:20:55 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: 
1.32    ! parser    230: typedef double (*math1_func_ptr)(double);
        !           231: static double round(double param) { return floor(param+0.5); }
        !           232: static double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); }
        !           233: 
        !           234: static void math1(Request& r, 
        !           235:                                  const String& method_name, MethodParams *params,
        !           236:                                  math1_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.32    ! parser    245: #define MATH1(name) \
1.31      parser    246:        static void _##name(Request& r, const String& method_name, MethodParams *params) {\
1.32    ! parser    247:                math1(r, method_name, params, &name);\
1.31      parser    248:        }
1.32    ! parser    249: #define MATH1P(name_parser, name_c) \
        !           250:        static void _##name_parser(Request& r, const String& method_name, MethodParams *params) {\
        !           251:                math1(r, method_name, params, &name_c);\
        !           252:        }
        !           253: MATH1(round);  MATH1(floor);   MATH1P(ceiling, ceil);
        !           254: MATH1P(abs, fabs);     MATH1(sign);
        !           255: MATH1(exp);    MATH1(log);     
        !           256: MATH1(sin);    MATH1(asin);    
        !           257: MATH1(cos);    MATH1(acos);    
        !           258: MATH1(tan);    MATH1(atan);
        !           259: MATH1(sqrt);
        !           260: 
        !           261: 
        !           262: typedef double (*math2_func_ptr)(double, double);
        !           263: static void math2(Request& r, 
        !           264:                                  const String& method_name, MethodParams *params,
        !           265:                                  math2_func_ptr func) {
        !           266:        Pool& pool=r.pool();
        !           267:        Value& a=params->get_junction(0, "parameter must be expression");
        !           268:        Value& b=params->get_junction(1, "parameter must be expression");
        !           269: 
        !           270:        Value& result=*new(pool) VDouble(pool, (*func)(
        !           271:                r.process(a).as_double(),
        !           272:                r.process(b).as_double()));
        !           273:        result.set_name(method_name);
        !           274:        r.write_no_lang(result);
        !           275: }
        !           276: 
        !           277: #define MATH2(name) \
        !           278:        static void _##name(Request& r, const String& method_name, MethodParams *params) {\
        !           279:                math2(r, method_name, params, &name);\
        !           280:        }
        !           281: MATH2(pow);
1.27      parser    282: 
                    283: 
1.5       paf       284: static void _connect(Request& r, const String&, MethodParams *params) {
1.1       paf       285:        Pool& pool=r.pool();
                    286: 
1.5       paf       287:        Value& url=params->get_no_junction(0, "url must not be code");
                    288:        Value& body_code=params->get_junction(1, "body must be code");
1.1       paf       289: 
1.19      parser    290:        Table *protocol2driver_and_client=
                    291:                static_cast<Table *>(r.classes_conf.get(OP->name()));
1.11      paf       292: 
1.1       paf       293:        // connect
                    294:        SQL_Connection& connection=SQL_driver_manager->get_connection(
1.19      parser    295:                url.as_string(), protocol2driver_and_client);
1.1       paf       296: 
                    297:        Exception rethrow_me;
                    298:        // remember/set current connection
                    299:        SQL_Connection *saved_connection=r.connection;
                    300:        r.connection=&connection;
                    301:        // execute body
                    302:        bool body_failed=false;  
                    303:        PTRY
                    304:                r.write_assign_lang(r.process(body_code));
                    305:        PCATCH(e) { // connect/process problem
                    306:                rethrow_me=e;  body_failed=true; 
                    307:        }
                    308:        PEND_CATCH
                    309: 
                    310:        bool finalizer_failed=false;
                    311:        PTRY
                    312:                // FINALLY
                    313:                if(body_failed)
                    314:                        connection.rollback();
                    315:                else
                    316:                        connection.commit();
                    317:        PCATCH(e) { // commit/rollback problem
                    318:                rethrow_me=e;  finalizer_failed=true; 
                    319:        }
                    320:        PEND_CATCH
                    321: 
                    322:        // close connection [cache it]
1.21      parser    323:        connection.close();
1.1       paf       324:        // recall current connection from remembered
                    325:        r.connection=saved_connection;
                    326: 
                    327:        if(body_failed || finalizer_failed) // were there an exception for us to rethrow?
                    328:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    329:                        rethrow_me.problem_source(),
                    330:                        rethrow_me.comment());
                    331: }
                    332: 
1.28      parser    333: static String *switch_data_name;
                    334: static String *default_value;
                    335: 
                    336: struct Switch_data {
                    337:        Value *searching;
                    338:        Value *found;
                    339:        Value *_default;
                    340: };
                    341: 
                    342: static void _switch(Request& r, const String&, MethodParams *params) {
                    343:        void *backup=r.classes_conf.get(*switch_data_name);
                    344:        Switch_data data={&r.process(params->get(0))};  
                    345:        r.classes_conf.put(*switch_data_name, &data);
                    346: 
                    347:        r.process(params->get_junction(1, "switch cases must be code")); // and ignore result
                    348: 
                    349:        r.classes_conf.put(*switch_data_name, backup);
                    350: 
                    351:        if(Value *code=data.found ? data.found : data._default)
                    352:                r.write_pass_lang(r.process(*code));
                    353: }
                    354: 
                    355: static void _case(Request& r, const String&, MethodParams *params) {
                    356:        Switch_data& data=*static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
                    357: 
                    358:        int count=params->size();
                    359:        Value *code=&params->get_junction(--count, "case result must be code");
                    360:        for(int i=0; i<count; i++) {
                    361:                Value& value=r.process(params->get(i));
                    362: 
                    363:                if(value.as_string() == *default_value) {
                    364:                        data._default=code;
                    365:                        break;
                    366:                }
                    367: 
                    368:                bool matches;
                    369:                if(data.searching->is_string())
                    370:                        matches=data.searching->as_string() == value.as_string();
                    371:                else
                    372:                        matches=data.searching->as_double() == value.as_double();
                    373: 
                    374:                if(matches) {
                    375:                        data.found=code;
                    376:                        break;
                    377:                }
                    378:        }
                    379: }
                    380: 
1.10      paf       381: // constructor
                    382: 
1.11      paf       383: MOP::MOP(Pool& apool) : Methoded(apool),
                    384:        main_sql_name(apool, MAIN_SQL_NAME),
                    385:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    386: {
1.10      paf       387:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.1       paf       388: 
1.28      parser    389:        switch_data_name=NEW String(pool(), SWITCH_DATA_NAME);
                    390:        default_value=NEW String(pool(), DEFAULT_VALUE);
                    391: 
1.1       paf       392:        // ^if(condition){code-when-true}
                    393:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       394:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       395: 
                    396:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       397:        add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
1.1       paf       398: 
                    399:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       400:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       401: 
                    402:        // ^process[code]
1.10      paf       403:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       404: 
                    405:        // ^rem{code}
1.26      parser    406:        add_native_method("rem", Method::CT_ANY, _rem, 1, 1000);
1.1       paf       407: 
                    408:        // ^while(condition){code}
1.10      paf       409:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       410: 
                    411:        // ^use[file]
1.10      paf       412:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       413: 
                    414:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
1.10      paf       415:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       416: 
                    417:        // ^eval(expr)
                    418:        // ^eval(expr)[format]
1.10      paf       419:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.1       paf       420: 
                    421: 
                    422:        // math functions
                    423: 
1.32    ! parser    424:        // ^FUNC(expr)  
        !           425: #define ADD_MATH1(name) \
        !           426:        add_native_method(#name, Method::CT_ANY, _##name, 1, 1)
        !           427: 
        !           428:        ADD_MATH1(round);       ADD_MATH1(floor);       ADD_MATH1(ceiling);
        !           429:        ADD_MATH1(abs); ADD_MATH1(sign);
        !           430:        ADD_MATH1(exp); ADD_MATH1(log); 
        !           431:        ADD_MATH1(sin); ADD_MATH1(asin);        
        !           432:        ADD_MATH1(cos); ADD_MATH1(acos);        
        !           433:        ADD_MATH1(tan); ADD_MATH1(atan);
        !           434:        ADD_MATH1(sqrt);
        !           435: 
        !           436: #define ADD_MATH2(name) \
        !           437:        add_native_method(#name, Method::CT_ANY, _##name, 2, 2)
        !           438: 
        !           439:        // ^pow(x;y)
        !           440:        ADD_MATH2(pow);
        !           441: 
1.1       paf       442: 
                    443:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       444:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    445: 
1.28      parser    446:        // switch
                    447: 
                    448:        // ^switch[value]{cases}
                    449:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    450: 
                    451:        // ^case[value]{code}
                    452:        add_native_method("case", Method::CT_ANY, _case, 2, 1000);
1.10      paf       453: }
1.11      paf       454: 
                    455: // constructor & configurator
1.1       paf       456: 
1.10      paf       457: Methoded *MOP_create(Pool& pool) {
1.11      paf       458:        return OP=new(pool) MOP(pool);
                    459: }
                    460: 
                    461: 
                    462: void MOP::configure_user(Request& r) {
                    463:        Pool& pool=r.pool();
                    464: 
                    465:        // $MAIN:SQL.drivers
                    466:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    467:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    468:                        if(Table *protocol2library=element->get_table())
                    469:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       470: }

E-mail: