Annotation of parser3/src/classes/root.C, revision 1.56

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

E-mail: