--- parser3/src/classes/Attic/root.C 2001/03/08 13:42:30 1.2 +++ parser3/src/classes/Attic/root.C 2001/03/12 18:19:36 1.28 @@ -1,31 +1,151 @@ /* -$Id: root.C,v 1.2 2001/03/08 13:42:30 paf Exp $ + Parser + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://design.ru/paf) + + $Id: root.C,v 1.28 2001/03/12 18:19:36 paf Exp $ */ +#include + #include "pa_request.h" +#include "_root.h" -static void _if(Request& r, Array& params) { +static void _if(Request& r, const String&, Array *params) { bool condition= - r.autocalc( - *static_cast(params.get(0)), - false/*don't make it string*/).get_bool(); - Value& value=r.autocalc( - *static_cast(params.get(condition?1:2)), - true/*make it string*/); - r.write(value); -} - -void construct_root_class(Request& request) { - Pool& pool=request.pool(); - String& IF_NAME=*new(pool) String(pool); - IF_NAME.APPEND_CONST("if"); - - Method& IF_METHOD=*new(pool) Method(pool, - IF_NAME, - 3/*numbered_params_count*/, - 0/*params_names*/, 0/*locals_names*/, - 0/*parser_code*/, _if - ); + r.process( + *static_cast(params->get(0)), + 0/*no name*/, + false/*don't intercept string*/).get_bool(); + if(condition) { + Value& value=r.process(*static_cast(params->get(1))); + r.write_pass_lang(value); + } else if(params->size()==3) { + Value& value=r.process(*static_cast(params->get(2))); + r.write_pass_lang(value); + } +} + +static void _untaint(Request& r, const String& method_name, Array *params) { + const String& lang_name=r.process(*static_cast(params->get(0))).as_string(); + String::Untaint_lang lang=static_cast( + untaint_lang_name2enum->get_int(lang_name)); + if(!lang) + R_THROW(0, 0, + &lang_name, + "invalid untaint language"); + + { + Temp_lang temp_lang(r, lang); + Value *vbody=static_cast(params->get(1)); + // forcing ^untaint[]{this param type} + r.fail_if_junction_(false, *vbody, + method_name, "body must be junction"); + + r.write_pass_lang(r.process(*vbody)); + } +} + + +static void _process(Request& r, const String& method_name, Array *params) { + // calculate pseudo file name of processed chars + // would be something like "/some/file(4) process" + char place[MAX_STRING]; +#ifndef NO_STRING_ORIGIN + const Origin& origin=method_name.origin(); + snprintf(place, MAX_STRING, "%s(%d) %s", + origin.file, 1+origin.line, + method_name.cstr()); +#else + strncpy(place, MAX_STRING, method_name.cstr()); +#endif + + VClass& self_class=*r.self->get_class(); + { + // temporary zero @main so to maybe-replace it in processed code + Temp_method temp_method(self_class, *main_method_name, 0); + + // evaluate source to process + const String& source= + r.process(*static_cast(params->get(0))).as_string(); + + // process source code, append processed methods to 'self' class + // maybe-define new @main + r.use_buf(source.cstr(), place, &self_class); + + // maybe-execute @main[] + if(const Method *method=self_class.get_method(*main_method_name)) { + // execute! + r.execute(*method->parser_code); + } + } +} + +static void _rem(Request& r, const String& method_name, Array *params) { + // forcing ^rem{this param type} + r.fail_if_junction_(false, *static_cast(params->get(0)), + method_name, "body must be junction"); +} + +static void _while(Request& r, const String& method_name, Array *params) { + Value& vcondition=*static_cast(params->get(0)); + // forcing ^while(this param type){} + r.fail_if_junction_(false, vcondition, + method_name, "condition must be junction"); + + Value& body=*static_cast(params->get(1)); + // forcing ^while(){this param type} + r.fail_if_junction_(false, body, + method_name, "body must be junction"); + + // while... + int endless_loop_count=0; + while(true) { + if(++endless_loop_count>=1973) // endless loop? + R_THROW(0, 0, + &method_name, + "endless loop detected"); + + bool condition= + r.process( + vcondition, + 0/*no name*/, + false/*don't intercept string*/).get_bool(); + if(!condition) // ...condition is true + break; + + // write processed body + r.write_pass_lang(r.process(body)); + } +} + +static void _use(Request& r, const String& method_name, Array *params) { + Value& vfile=*static_cast(params->get(0)); + // forcing ^rem{this param type} + r.fail_if_junction_(true, vfile, + method_name, "file name must not be junction"); + + char *file=vfile.as_string().cstr(); + r.use_file(r.absolute(file)); +} + +void initialize_root_class(Pool& pool, VClass& vclass) { + // ^if(condition){code-when-true} + // ^if(condition){code-when-true}{code-when-false} + vclass.add_native_method("if", _if, 2, 3); + + // ^untaint[as-is|sql|js|html|html-typo]{code} + vclass.add_native_method("untaint", _untaint, 2, 2); + + // ^process[code] + vclass.add_native_method("process", _process, 1, 1); + + // ^rem{code} + vclass.add_native_method("rem", _rem, 1, 1); + + // ^while(condition){code} + vclass.add_native_method("while", _while, 2, 2); - request.root_class.add_method(IF_NAME, IF_METHOD); + // ^use[file] + vclass.add_native_method("use", _use, 1, 1); }