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