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