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

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

E-mail: