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

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.25    ! paf         6:        $Id: root.C,v 1.24 2001/03/12 13:13:19 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}
                     42:                if(!vbody->get_junction())
                     43:                        R_THROW(0, 0,
1.22      paf        44:                        &method_name,
1.18      paf        45:                        "body must be junction");
1.24      paf        46:                
                     47:                r.write_pass_lang(r.process(*vbody));
                     48:        }
1.15      paf        49: }
                     50:        
                     51: 
1.22      paf        52: static void _process(Request& r, const String& method_name, Array *params) {
1.20      paf        53:        // calculate pseudo file name of processed chars
                     54:        // would be something like "/some/file(4) process"
1.18      paf        55:        char place[MAX_STRING];
                     56: #ifndef NO_STRING_ORIGIN
1.25    ! paf        57:        const Origin& origin=method_name.origin();
1.18      paf        58:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                     59:                origin.file, 1+origin.line,
1.22      paf        60:                method_name.cstr());
1.18      paf        61: #else
1.22      paf        62:        strncpy(place, MAX_STRING, method_name.cstr());
1.18      paf        63: #endif 
                     64: 
1.20      paf        65:        VClass& self_class=*r.self->get_class();
1.22      paf        66:        {
                     67:                // temporary zero @main so to maybe-replace it in processed code
                     68:                Temp_method temp_method(self_class, *main_method_name, 0);
                     69:                
1.25    ! paf        70:                // evaluate source to process
        !            71:                const String& source=
        !            72:                        r.process(*static_cast<Value *>(params->get(0))).as_string();
        !            73: 
1.22      paf        74:                // process source code, append processed methods to 'self' class
                     75:                // maybe-define new @main
                     76:                r.use_buf(source.cstr(), place, &self_class);
                     77:                
                     78:                // maybe-execute @main[]
                     79:                if(const Method *method=self_class.get_method(*main_method_name)) {
                     80:                        // execute!     
                     81:                        r.execute(*method->parser_code);
                     82:                }
1.18      paf        83:        }
                     84: }
                     85:        
                     86: 
1.7       paf        87: void initialize_root_class(Pool& pool, VClass& vclass) {
1.15      paf        88:        // ^if(condition){code-when-true}
                     89:        // ^if(condition){code-when-true}{code-when-false}
                     90:        vclass.add_native_method("if", _if, 2, 3);
                     91: 
                     92:        // ^untaint[as-is|sql|js|html|html-typo]{code}
                     93:        vclass.add_native_method("untaint", _untaint, 2, 2);
1.18      paf        94: 
                     95:        // ^process[code]
                     96:        vclass.add_native_method("process", _process, 1, 1);
1.1       paf        97: }

E-mail: