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