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

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.7     ! paf         8:        $Id: op.C,v 1.6 2001/04/15 13:12:17 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}
1.5       paf       236: static void _connect(Request& r, const String&, MethodParams *params) {
1.1       paf       237:        Pool& pool=r.pool();
                    238: 
1.5       paf       239:        Value& url=params->get_no_junction(0, "url must not be code");
                    240:        Value& body_code=params->get_junction(1, "body must be code");
1.1       paf       241: 
                    242:        // connect
                    243:        SQL_Connection& connection=SQL_driver_manager->get_connection(
                    244:                url.as_string(), r.protocol2library);
                    245: 
                    246:        Exception rethrow_me;
                    247:        // remember/set current connection
                    248:        SQL_Connection *saved_connection=r.connection;
                    249:        r.connection=&connection;
                    250:        // execute body
                    251:        bool body_failed=false;  
                    252:        PTRY
                    253:                r.write_assign_lang(r.process(body_code));
                    254:        PCATCH(e) { // connect/process problem
                    255:                rethrow_me=e;  body_failed=true; 
                    256:        }
                    257:        PEND_CATCH
                    258: 
                    259:        bool finalizer_failed=false;
                    260:        PTRY
                    261:                // FINALLY
                    262:                if(body_failed)
                    263:                        connection.rollback();
                    264:                else
                    265:                        connection.commit();
                    266:        PCATCH(e) { // commit/rollback problem
                    267:                rethrow_me=e;  finalizer_failed=true; 
                    268:        }
                    269:        PEND_CATCH
                    270: 
                    271:        // close connection [cache it]
                    272:        connection.close();
                    273:        // recall current connection from remembered
                    274:        r.connection=saved_connection;
                    275: 
                    276:        if(body_failed || finalizer_failed) // were there an exception for us to rethrow?
                    277:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    278:                        rethrow_me.problem_source(),
                    279:                        rethrow_me.comment());
                    280: }
                    281: 
                    282: // initialize
                    283: 
1.2       paf       284: void initialize_op_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       285:        // ^if(condition){code-when-true}
                    286:        // ^if(condition){code-when-true}{code-when-false}
                    287:        vclass.add_native_method("if", Method::CT_ANY, _if, 2, 3);
                    288: 
                    289:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
                    290:        vclass.add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
                    291: 
                    292:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
                    293:        vclass.add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
                    294: 
                    295:        // ^process[code]
                    296:        vclass.add_native_method("process", Method::CT_ANY, _process, 1, 1);
                    297: 
                    298:        // ^rem{code}
                    299:        vclass.add_native_method("rem", Method::CT_ANY, _rem, 1, 1);
                    300: 
                    301:        // ^while(condition){code}
                    302:        vclass.add_native_method("while", Method::CT_ANY, _while, 2, 2);
                    303: 
                    304:        // ^use[file]
                    305:        vclass.add_native_method("use", Method::CT_ANY, _use, 1, 1);
                    306: 
                    307:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
                    308:        vclass.add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
                    309: 
                    310:        // ^eval(expr)
                    311:        // ^eval(expr)[format]
                    312:        vclass.add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
                    313: 
                    314: 
                    315:        // math functions
                    316: 
                    317:        // ^round(expr) 
                    318:        vclass.add_native_method("round", Method::CT_ANY, _round, 1, 1);
                    319: 
                    320:        // ^floor(expr) 
                    321:        vclass.add_native_method("floor", Method::CT_ANY, _floor, 1, 1);
                    322: 
                    323:        // ^ceiling(expr)       
                    324:        vclass.add_native_method("ceiling", Method::CT_ANY, _ceiling, 1, 1);
                    325: 
                    326:        // ^abs(expr)   
                    327:        vclass.add_native_method("abs", Method::CT_ANY, _abs, 1, 1);
                    328: 
                    329:        // ^sign(expr)
                    330:        vclass.add_native_method("sign", Method::CT_ANY, _sign, 1, 1);
                    331: 
                    332:        
                    333:        // connect
                    334: 
                    335:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
                    336:        vclass.add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    337: 
                    338: }

E-mail: