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

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

E-mail: