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

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

E-mail: