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

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

E-mail: