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

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.28    ! paf         6:        $Id: root.C,v 1.27 2001/03/12 18:13:48 paf Exp $
1.1       paf         7: */
                      8: 
1.18      paf         9: #include <string.h>
                     10: 
1.1       paf        11: #include "pa_request.h"
1.8       paf        12: #include "_root.h"
1.1       paf        13: 
1.18      paf        14: static void _if(Request& r, const String&, Array *params) {
1.2       paf        15:        bool condition=
1.18      paf        16:                r.process(
1.6       paf        17:                        *static_cast<Value *>(params->get(0)), 
1.11      paf        18:                        0/*no name*/,
1.14      paf        19:                        false/*don't intercept string*/).get_bool();
1.6       paf        20:        if(condition) {
1.18      paf        21:                Value& value=r.process(*static_cast<Value *>(params->get(1)));
1.22      paf        22:                r.write_pass_lang(value);
1.6       paf        23:        } else if(params->size()==3) {
1.18      paf        24:                Value& value=r.process(*static_cast<Value *>(params->get(2)));
1.22      paf        25:                r.write_pass_lang(value);
1.6       paf        26:        }
1.1       paf        27: }
                     28: 
1.22      paf        29: static void _untaint(Request& r, const String& method_name, Array *params) {
1.18      paf        30:        const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.15      paf        31:        String::Untaint_lang lang=static_cast<String::Untaint_lang>(
1.23      paf        32:                untaint_lang_name2enum->get_int(lang_name));
1.15      paf        33:        if(!lang)
                     34:                R_THROW(0, 0,
                     35:                        &lang_name,
1.18      paf        36:                        "invalid untaint language");
1.15      paf        37: 
1.24      paf        38:        {
                     39:                Temp_lang temp_lang(r, lang);
                     40:                Value *vbody=static_cast<Value *>(params->get(1));
                     41:                // forcing ^untaint[]{this param type}
1.26      paf        42:                r.fail_if_junction_(false, *vbody, 
                     43:                        method_name, "body must be junction");
1.24      paf        44:                
                     45:                r.write_pass_lang(r.process(*vbody));
                     46:        }
1.15      paf        47: }
                     48:        
                     49: 
1.22      paf        50: static void _process(Request& r, const String& method_name, Array *params) {
1.20      paf        51:        // calculate pseudo file name of processed chars
                     52:        // would be something like "/some/file(4) process"
1.18      paf        53:        char place[MAX_STRING];
                     54: #ifndef NO_STRING_ORIGIN
1.25      paf        55:        const Origin& origin=method_name.origin();
1.18      paf        56:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                     57:                origin.file, 1+origin.line,
1.22      paf        58:                method_name.cstr());
1.18      paf        59: #else
1.22      paf        60:        strncpy(place, MAX_STRING, method_name.cstr());
1.18      paf        61: #endif 
                     62: 
1.20      paf        63:        VClass& self_class=*r.self->get_class();
1.22      paf        64:        {
                     65:                // temporary zero @main so to maybe-replace it in processed code
                     66:                Temp_method temp_method(self_class, *main_method_name, 0);
                     67:                
1.25      paf        68:                // evaluate source to process
                     69:                const String& source=
                     70:                        r.process(*static_cast<Value *>(params->get(0))).as_string();
                     71: 
1.22      paf        72:                // process source code, append processed methods to 'self' class
                     73:                // maybe-define new @main
                     74:                r.use_buf(source.cstr(), place, &self_class);
                     75:                
                     76:                // maybe-execute @main[]
                     77:                if(const Method *method=self_class.get_method(*main_method_name)) {
                     78:                        // execute!     
                     79:                        r.execute(*method->parser_code);
                     80:                }
1.18      paf        81:        }
                     82: }
                     83:        
1.26      paf        84: static void _rem(Request& r, const String& method_name, Array *params) {
1.27      paf        85:        // forcing ^rem{this param type}
1.26      paf        86:        r.fail_if_junction_(false, *static_cast<Value *>(params->get(0)), 
                     87:                method_name, "body must be junction");
                     88: }
1.18      paf        89: 
1.27      paf        90: static void _while(Request& r, const String& method_name, Array *params) {
                     91:        Value& vcondition=*static_cast<Value *>(params->get(0));
                     92:        // forcing ^while(this param type){}
                     93:        r.fail_if_junction_(false, vcondition, 
                     94:                method_name, "condition must be junction");
                     95:        
                     96:        Value& body=*static_cast<Value *>(params->get(1));
                     97:        // forcing ^while(){this param type}
                     98:        r.fail_if_junction_(false, body, 
                     99:                method_name, "body must be junction");
                    100: 
                    101:        // while...
                    102:        int endless_loop_count=0;
                    103:        while(true) {
                    104:                if(++endless_loop_count>=1973) // endless loop?
                    105:                        R_THROW(0, 0,
                    106:                                &method_name,
                    107:                                "endless loop detected");
                    108: 
                    109:                bool condition=
                    110:                        r.process(
                    111:                                vcondition, 
                    112:                                0/*no name*/,
                    113:                                false/*don't intercept string*/).get_bool();
                    114:                if(!condition) // ...condition is true
                    115:                        break;
                    116: 
                    117:                // write processed body
                    118:                r.write_pass_lang(r.process(body));
                    119:        }
                    120: }
                    121: 
1.28    ! paf       122: static void _use(Request& r, const String& method_name, Array *params) {
        !           123:        Value& vfile=*static_cast<Value *>(params->get(0));
        !           124:        // forcing ^rem{this param type}
        !           125:        r.fail_if_junction_(true, vfile, 
        !           126:                method_name, "file name must not be junction");
        !           127: 
        !           128:        char *file=vfile.as_string().cstr();
        !           129:        r.use_file(r.absolute(file));
        !           130: }
        !           131: 
1.7       paf       132: void initialize_root_class(Pool& pool, VClass& vclass) {
1.15      paf       133:        // ^if(condition){code-when-true}
                    134:        // ^if(condition){code-when-true}{code-when-false}
                    135:        vclass.add_native_method("if", _if, 2, 3);
                    136: 
                    137:        // ^untaint[as-is|sql|js|html|html-typo]{code}
                    138:        vclass.add_native_method("untaint", _untaint, 2, 2);
1.18      paf       139: 
                    140:        // ^process[code]
                    141:        vclass.add_native_method("process", _process, 1, 1);
1.26      paf       142: 
                    143:        // ^rem{code}
                    144:        vclass.add_native_method("rem", _rem, 1, 1);
1.27      paf       145: 
                    146:        // ^while(condition){code}
                    147:        vclass.add_native_method("while", _while, 2, 2);
1.28    ! paf       148: 
        !           149:        // ^use[file]
        !           150:        vclass.add_native_method("use", _use, 1, 1);
1.1       paf       151: }

E-mail: