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

1.1       paf         1: /** @file
                      2:        Parser: @b ROOT parser class.
                      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.6     ! paf         8:        $Id: op.C,v 1.5 2001/04/15 12:32:57 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: #include <math.h>
                     13: 
                     14: #include "pa_common.h"
                     15: #include "pa_request.h"
1.2       paf        16: #include "_op.h"
1.1       paf        17: #include "pa_vint.h"
                     18: #include "pa_sql_connection.h"
                     19: 
1.5       paf        20: static void _if(Request& r, const String&, MethodParams *params) {
                     21:        Value& condition_code=params->get(0);
1.1       paf        22: 
                     23:        bool condition=r.process(condition_code, 
                     24:                0/*no name*/,
                     25:                false/*don't intercept string*/).as_bool();
1.6     ! paf        26:        if(condition)
1.5       paf        27:                r.write_pass_lang(r.process(params->get(1)));
1.6     ! paf        28:        else if(params->size()==3)
1.5       paf        29:                r.write_pass_lang(r.process(params->get(2)));
1.1       paf        30: }
                     31: 
1.5       paf        32: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        33:        Pool& pool=r.pool();
                     34: 
1.5       paf        35:        const String& lang_name=r.process(params->get(0)).as_string();
1.1       paf        36:        String::Untaint_lang lang=static_cast<String::Untaint_lang>(
                     37:                untaint_lang_name2enum->get_int(lang_name));
                     38:        if(!lang)
                     39:                PTHROW(0, 0,
                     40:                        &lang_name,
                     41:                        "invalid untaint language");
                     42: 
                     43:        {
1.5       paf        44:                Value& vbody=params->get_junction(1, "body must be code");
1.1       paf        45:                
                     46:                Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.5       paf        47:                r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1       paf        48:        }
                     49: }
                     50: 
1.5       paf        51: static void _taint(Request& r, const String&, MethodParams *params) {
1.1       paf        52:        Pool& pool=r.pool();
                     53: 
                     54:        String::Untaint_lang lang;
                     55:        if(params->size()==1)
                     56:                lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
                     57:        else {
1.3       paf        58:                const String& lang_name=
1.5       paf        59:                        r.process(params->get(0)).as_string();
1.1       paf        60:                lang=static_cast<String::Untaint_lang>(
                     61:                        untaint_lang_name2enum->get_int(lang_name));
                     62:                if(!lang)
                     63:                        PTHROW(0, 0,
1.3       paf        64:                                &lang_name,
                     65:                                "invalid taint language");
1.1       paf        66:        }
                     67: 
                     68:        {
1.5       paf        69:                Value& vbody=params->get_no_junction(params->size()-1, "body must not be code");
1.1       paf        70:                
                     71:                String result(r.pool());
                     72:                result.append(
                     73:                        vbody.as_string(),  // process marking tainted with that lang
                     74:                        lang, true);  // force result language to specified
                     75:                r.write_pass_lang(result);
                     76:        }
                     77: }
                     78: 
1.5       paf        79: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        80:        // calculate pseudo file name of processed chars
                     81:        // would be something like "/some/file(4) process"
                     82:        char place[MAX_STRING];
                     83: #ifndef NO_STRING_ORIGIN
                     84:        const Origin& origin=method_name.origin();
                     85:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                     86:                origin.file, 1+origin.line,
                     87:                method_name.cstr());
                     88: #else
                     89:        strncpy(place, MAX_STRING, method_name.cstr());
                     90: #endif 
                     91: 
                     92:        VStateless_class& self_class=*r.self->get_class();
                     93:        {
                     94:                // temporary zero @main so to maybe-replace it in processed code
                     95:                Temp_method temp_method_main(self_class, *main_method_name, 0);
                     96:                // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
                     97:                Temp_method temp_method_auto(self_class, *auto_method_name, 0);
                     98:                
                     99:                // evaluate source to process
                    100:                const String& source=
1.5       paf       101:                        r.process(params->get(0)).as_string();
1.1       paf       102: 
                    103:                // process source code, append processed methods to 'self' class
                    104:                // maybe-define new @main
                    105:                r.use_buf(source.cstr(), place, &self_class);
                    106:                
                    107:                // maybe-execute @main[]
                    108:                if(const Method *method=self_class.get_method(*main_method_name)) {
                    109:                        // execute!     
                    110:                        r.execute(*method->parser_code);
                    111:                }
                    112:        }
                    113: }
                    114:        
1.5       paf       115: static void _rem(Request& r, const String&, MethodParams *params) {
                    116:        params->get_junction(0, "body must be code");
1.1       paf       117: }
                    118: 
1.5       paf       119: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       120:        Pool& pool=r.pool();
                    121: 
1.5       paf       122:        Value& vcondition=params->get_junction(0, "condition must be expression");
                    123:        Value& body=params->get_junction(1, "body must be code");
1.1       paf       124: 
                    125:        // while...
                    126:        int endless_loop_count=0;
                    127:        while(true) {
                    128:                if(++endless_loop_count>=1973) // endless loop?
                    129:                        PTHROW(0, 0,
                    130:                                &method_name,
                    131:                                "endless loop detected");
                    132: 
                    133:                bool condition=
                    134:                        r.process(
                    135:                                vcondition, 
                    136:                                0/*no name*/,
                    137:                                false/*don't intercept string*/).as_bool();
                    138:                if(!condition) // ...condition is true
                    139:                        break;
                    140: 
                    141:                // write processed body
                    142:                r.write_pass_lang(r.process(body));
                    143:        }
                    144: }
                    145: 
1.5       paf       146: static void _use(Request& r, const String& method_name, MethodParams *params) {
                    147:        Value& vfile=params->get_no_junction(0, "file name must not be code");
1.1       paf       148:        r.use_file(r.absolute(vfile.as_string()));
                    149: }
                    150: 
1.4       paf       151: /// ^for[i;from-number;to-number-inclusive]{code}[delim]
1.5       paf       152: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       153:        Pool& pool=r.pool();
1.5       paf       154:        const String& var_name=r.process(params->get(0)).as_string();
                    155:        int from=(int)r.process(params->get(1)).as_double();
                    156:        int to=(int)r.process(params->get(2)).as_double();
                    157:        Value& body_code=params->get_junction(3, "body must be code");
                    158:        Value *delim_code=params->size()==3+1+1?&params->get(3+1):0;
1.1       paf       159: 
                    160:        bool need_delim=false;
                    161:        VInt *vint=new(pool) VInt(pool, 0);
                    162:        int endless_loop_count=0;
                    163:        for(int i=from; i<=to; i++) {
                    164:                if(++endless_loop_count>=2001) // endless loop?
                    165:                        PTHROW(0, 0,
                    166:                                &method_name,
                    167:                                "endless loop detected");
                    168:                vint->set_int(i);
1.4       paf       169:                r.root->put_element(var_name, vint);
1.1       paf       170: 
                    171:                Value& processed_body=r.process(body_code);
                    172:                if(delim_code) { // delimiter set?
                    173:                        const String *string=processed_body.get_string();
                    174:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    175:                                r.write_pass_lang(r.process(*delim_code));
                    176:                        need_delim=true;
                    177:                }
                    178:                r.write_pass_lang(processed_body);
                    179:        }
                    180: }
                    181: 
1.5       paf       182: static void _eval(Request& r, const String&, MethodParams *params) {
                    183:        Value& expr=params->get_junction(0, "need expression");
1.1       paf       184:        // evaluate expresion
                    185:        Value *result=r.process(expr, 
                    186:                0/*no name*/,
                    187:                true/*don't intercept string*/).as_expr_result();
                    188:        if(params->size()==2) {
1.5       paf       189:                Value& fmt=params->get_no_junction(1, "fmt must not be code");
1.1       paf       190: 
                    191:                Pool& pool=r.pool();
                    192:                String& string=*new(pool) String(pool);
                    193:                string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
                    194:                result=new(pool) VString(string);
                    195:        }
                    196:        r.write_no_lang(*result);
                    197: }
                    198: 
                    199: 
                    200: typedef double (*math_one_double_op_func_ptr)(double);
                    201: static double round(double op) { return floor(op+0.5); }
                    202: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
                    203: 
                    204: static void double_one_op(
                    205:                                                                Request& r, 
1.5       paf       206:                                                                const String& method_name, MethodParams *params,
1.1       paf       207:                                                                math_one_double_op_func_ptr func) {
                    208:        Pool& pool=r.pool();
1.5       paf       209:        Value& param=params->get_junction(0, "parameter must be expression");
1.1       paf       210: 
                    211:        Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
                    212:        r.write_no_lang(result);
                    213: }
                    214: 
1.5       paf       215: static void _round(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       216:        double_one_op(r, method_name, params,   &round);
                    217: }
                    218: 
1.5       paf       219: static void _floor(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       220:        double_one_op(r, method_name, params,   &floor);
                    221: }
                    222: 
1.5       paf       223: static void _ceiling(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       224:        double_one_op(r, method_name, params,   &ceil);
                    225: }
                    226: 
1.5       paf       227: static void _abs(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       228:        double_one_op(r, method_name, params,   &fabs);
                    229: }
                    230: 
1.5       paf       231: static void _sign(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       232:        double_one_op(r, method_name, params,   &sign);
                    233: }
                    234: 
                    235: /// ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
                    236: /**
                    237:        @test make params not Array but something with useful method for extracting,
                    238:        with typecast and junction/not test
                    239: */
1.5       paf       240: static void _connect(Request& r, const String&, MethodParams *params) {
1.1       paf       241:        Pool& pool=r.pool();
                    242: 
1.5       paf       243:        Value& url=params->get_no_junction(0, "url must not be code");
                    244:        Value& body_code=params->get_junction(1, "body must be code");
1.1       paf       245: 
                    246:        // connect
                    247:        SQL_Connection& connection=SQL_driver_manager->get_connection(
                    248:                url.as_string(), r.protocol2library);
                    249: 
                    250:        Exception rethrow_me;
                    251:        // remember/set current connection
                    252:        SQL_Connection *saved_connection=r.connection;
                    253:        r.connection=&connection;
                    254:        // execute body
                    255:        bool body_failed=false;  
                    256:        PTRY
                    257:                r.write_assign_lang(r.process(body_code));
                    258:        PCATCH(e) { // connect/process problem
                    259:                rethrow_me=e;  body_failed=true; 
                    260:        }
                    261:        PEND_CATCH
                    262: 
                    263:        bool finalizer_failed=false;
                    264:        PTRY
                    265:                // FINALLY
                    266:                if(body_failed)
                    267:                        connection.rollback();
                    268:                else
                    269:                        connection.commit();
                    270:        PCATCH(e) { // commit/rollback problem
                    271:                rethrow_me=e;  finalizer_failed=true; 
                    272:        }
                    273:        PEND_CATCH
                    274: 
                    275:        // close connection [cache it]
                    276:        connection.close();
                    277:        // recall current connection from remembered
                    278:        r.connection=saved_connection;
                    279: 
                    280:        if(body_failed || finalizer_failed) // were there an exception for us to rethrow?
                    281:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    282:                        rethrow_me.problem_source(),
                    283:                        rethrow_me.comment());
                    284: }
                    285: 
                    286: // initialize
                    287: 
1.2       paf       288: void initialize_op_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       289:        // ^if(condition){code-when-true}
                    290:        // ^if(condition){code-when-true}{code-when-false}
                    291:        vclass.add_native_method("if", Method::CT_ANY, _if, 2, 3);
                    292: 
                    293:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
                    294:        vclass.add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
                    295: 
                    296:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
                    297:        vclass.add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
                    298: 
                    299:        // ^process[code]
                    300:        vclass.add_native_method("process", Method::CT_ANY, _process, 1, 1);
                    301: 
                    302:        // ^rem{code}
                    303:        vclass.add_native_method("rem", Method::CT_ANY, _rem, 1, 1);
                    304: 
                    305:        // ^while(condition){code}
                    306:        vclass.add_native_method("while", Method::CT_ANY, _while, 2, 2);
                    307: 
                    308:        // ^use[file]
                    309:        vclass.add_native_method("use", Method::CT_ANY, _use, 1, 1);
                    310: 
                    311:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
                    312:        vclass.add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
                    313: 
                    314:        // ^eval(expr)
                    315:        // ^eval(expr)[format]
                    316:        vclass.add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
                    317: 
                    318: 
                    319:        // math functions
                    320: 
                    321:        // ^round(expr) 
                    322:        vclass.add_native_method("round", Method::CT_ANY, _round, 1, 1);
                    323: 
                    324:        // ^floor(expr) 
                    325:        vclass.add_native_method("floor", Method::CT_ANY, _floor, 1, 1);
                    326: 
                    327:        // ^ceiling(expr)       
                    328:        vclass.add_native_method("ceiling", Method::CT_ANY, _ceiling, 1, 1);
                    329: 
                    330:        // ^abs(expr)   
                    331:        vclass.add_native_method("abs", Method::CT_ANY, _abs, 1, 1);
                    332: 
                    333:        // ^sign(expr)
                    334:        vclass.add_native_method("sign", Method::CT_ANY, _sign, 1, 1);
                    335: 
                    336:        
                    337:        // connect
                    338: 
                    339:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
                    340:        vclass.add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    341: 
                    342: }

E-mail: