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

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

E-mail: