Annotation of parser3/src/classes/op.C, revision 1.30
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 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.30 ! parser 8: static const char *RCSId="$Id: double.C,v 1.31 2001/06/28 07:41:59 parser Exp $";
1.1 paf 9:
1.13 paf 10: #include "classes.h"
1.1 paf 11: #include "pa_config_includes.h"
12: #include "pa_common.h"
13: #include "pa_request.h"
14: #include "pa_vint.h"
15: #include "pa_sql_connection.h"
16:
1.18 parser 17: // limits
18:
19: #define MAX_LOOPS 10000
20:
1.10 paf 21: // defines
22:
23: #define OP_CLASS_NAME "OP"
24:
1.11 paf 25: #define MAIN_SQL_NAME "SQL"
26: #define MAIN_SQL_DRIVERS_NAME "drivers"
27:
1.28 parser 28: #define SWITCH_DATA_NAME "SWITCH-DATA"
29: #define DEFAULT_VALUE "DEFAULT"
30:
1.11 paf 31: // local variable
32:
33: static Methoded *OP;
34:
1.10 paf 35: // class
36:
37: class MOP : public Methoded {
38: public:
39: MOP(Pool& pool);
1.11 paf 40: public: // Methoded
1.10 paf 41: bool used_directly() { return true; }
1.11 paf 42: void configure_user(Request& r);
43: private:
44: String main_sql_name;
45: String main_sql_drivers_name;
1.10 paf 46: };
47:
48: // methods
49:
1.5 paf 50: static void _if(Request& r, const String&, MethodParams *params) {
51: Value& condition_code=params->get(0);
1.1 paf 52:
53: bool condition=r.process(condition_code,
54: 0/*no name*/,
55: false/*don't intercept string*/).as_bool();
1.6 paf 56: if(condition)
1.25 parser 57: r.write_pass_lang(r.process(params->get_junction(1, "'then' parameter must be code")));
1.6 paf 58: else if(params->size()==3)
1.25 parser 59: r.write_pass_lang(r.process(params->get_junction(2, "'else' parameter must be code")));
1.1 paf 60: }
61:
1.5 paf 62: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 63: Pool& pool=r.pool();
64:
1.5 paf 65: const String& lang_name=r.process(params->get(0)).as_string();
1.1 paf 66: String::Untaint_lang lang=static_cast<String::Untaint_lang>(
67: untaint_lang_name2enum->get_int(lang_name));
68: if(!lang)
69: PTHROW(0, 0,
70: &lang_name,
71: "invalid untaint language");
72:
73: {
1.5 paf 74: Value& vbody=params->get_junction(1, "body must be code");
1.1 paf 75:
76: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.5 paf 77: r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1 paf 78: }
79: }
80:
1.5 paf 81: static void _taint(Request& r, const String&, MethodParams *params) {
1.1 paf 82: Pool& pool=r.pool();
83:
84: String::Untaint_lang lang;
85: if(params->size()==1)
86: lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
87: else {
1.3 paf 88: const String& lang_name=
1.5 paf 89: r.process(params->get(0)).as_string();
1.1 paf 90: lang=static_cast<String::Untaint_lang>(
91: untaint_lang_name2enum->get_int(lang_name));
92: if(!lang)
93: PTHROW(0, 0,
1.3 paf 94: &lang_name,
95: "invalid taint language");
1.1 paf 96: }
97:
98: {
1.5 paf 99: Value& vbody=params->get_no_junction(params->size()-1, "body must not be code");
1.1 paf 100:
101: String result(r.pool());
102: result.append(
103: vbody.as_string(), // process marking tainted with that lang
104: lang, true); // force result language to specified
105: r.write_pass_lang(result);
106: }
107: }
108:
1.5 paf 109: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 110: // calculate pseudo file name of processed chars
111: // would be something like "/some/file(4) process"
112: char place[MAX_STRING];
113: #ifndef NO_STRING_ORIGIN
114: const Origin& origin=method_name.origin();
115: snprintf(place, MAX_STRING, "%s(%d) %s",
116: origin.file, 1+origin.line,
117: method_name.cstr());
118: #else
119: strncpy(place, MAX_STRING, method_name.cstr());
120: #endif
121:
122: VStateless_class& self_class=*r.self->get_class();
123: {
124: // temporary zero @main so to maybe-replace it in processed code
125: Temp_method temp_method_main(self_class, *main_method_name, 0);
126: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
127: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
128:
129: // evaluate source to process
130: const String& source=
1.5 paf 131: r.process(params->get(0)).as_string();
1.1 paf 132:
133: // process source code, append processed methods to 'self' class
134: // maybe-define new @main
135: r.use_buf(source.cstr(), place, &self_class);
136:
137: // maybe-execute @main[]
138: if(const Method *method=self_class.get_method(*main_method_name)) {
139: // execute!
140: r.execute(*method->parser_code);
141: }
142: }
143: }
144:
1.5 paf 145: static void _rem(Request& r, const String&, MethodParams *params) {
146: params->get_junction(0, "body must be code");
1.1 paf 147: }
148:
1.5 paf 149: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 150: Pool& pool=r.pool();
151:
1.5 paf 152: Value& vcondition=params->get_junction(0, "condition must be expression");
153: Value& body=params->get_junction(1, "body must be code");
1.1 paf 154:
155: // while...
156: int endless_loop_count=0;
157: while(true) {
1.18 parser 158: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.1 paf 159: PTHROW(0, 0,
160: &method_name,
161: "endless loop detected");
162:
163: bool condition=
164: r.process(
165: vcondition,
166: 0/*no name*/,
167: false/*don't intercept string*/).as_bool();
168: if(!condition) // ...condition is true
169: break;
170:
171: // write processed body
172: r.write_pass_lang(r.process(body));
173: }
174: }
175:
1.5 paf 176: static void _use(Request& r, const String& method_name, MethodParams *params) {
177: Value& vfile=params->get_no_junction(0, "file name must not be code");
1.1 paf 178: r.use_file(r.absolute(vfile.as_string()));
179: }
180:
1.5 paf 181: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 182: Pool& pool=r.pool();
1.5 paf 183: const String& var_name=r.process(params->get(0)).as_string();
1.18 parser 184: int from=r.process(params->get(1)).as_int();
185: int to=r.process(params->get(2)).as_int();
1.5 paf 186: Value& body_code=params->get_junction(3, "body must be code");
187: Value *delim_code=params->size()==3+1+1?¶ms->get(3+1):0;
1.1 paf 188:
189: bool need_delim=false;
190: VInt *vint=new(pool) VInt(pool, 0);
191: int endless_loop_count=0;
192: for(int i=from; i<=to; i++) {
1.18 parser 193: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.1 paf 194: PTHROW(0, 0,
195: &method_name,
196: "endless loop detected");
197: vint->set_int(i);
1.4 paf 198: r.root->put_element(var_name, vint);
1.1 paf 199:
200: Value& processed_body=r.process(body_code);
201: if(delim_code) { // delimiter set?
202: const String *string=processed_body.get_string();
203: if(need_delim && string && string->size()) // need delim & iteration produced string?
204: r.write_pass_lang(r.process(*delim_code));
205: need_delim=true;
206: }
207: r.write_pass_lang(processed_body);
208: }
209: }
210:
1.15 paf 211: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.5 paf 212: Value& expr=params->get_junction(0, "need expression");
1.1 paf 213: // evaluate expresion
214: Value *result=r.process(expr,
1.16 paf 215: 0/*no name YET*/,
1.1 paf 216: true/*don't intercept string*/).as_expr_result();
217: if(params->size()==2) {
1.5 paf 218: Value& fmt=params->get_no_junction(1, "fmt must not be code");
1.1 paf 219:
220: Pool& pool=r.pool();
221: String& string=*new(pool) String(pool);
222: string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
223: result=new(pool) VString(string);
224: }
1.16 paf 225: result->set_name(method_name);
1.1 paf 226: r.write_no_lang(*result);
227: }
228:
229:
230: typedef double (*math_one_double_op_func_ptr)(double);
231: static double round(double op) { return floor(op+0.5); }
232: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
233:
1.17 paf 234: static void double_one_op(Request& r,
235: const String& method_name, MethodParams *params,
236: math_one_double_op_func_ptr func) {
1.1 paf 237: Pool& pool=r.pool();
1.5 paf 238: Value& param=params->get_junction(0, "parameter must be expression");
1.1 paf 239:
240: Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
1.16 paf 241: result.set_name(method_name);
1.1 paf 242: r.write_no_lang(result);
243: }
244:
1.5 paf 245: static void _round(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 246: double_one_op(r, method_name, params, &round);
247: }
248:
1.5 paf 249: static void _floor(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 250: double_one_op(r, method_name, params, &floor);
251: }
252:
1.5 paf 253: static void _ceiling(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 254: double_one_op(r, method_name, params, &ceil);
255: }
256:
1.5 paf 257: static void _abs(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 258: double_one_op(r, method_name, params, &fabs);
259: }
260:
1.5 paf 261: static void _sign(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 262: double_one_op(r, method_name, params, &sign);
263: }
264:
1.27 parser 265: static void _log(Request& r, const String& method_name, MethodParams *params) {
266: double_one_op(r, method_name, params, &log);
267: }
268:
269: static void _exp(Request& r, const String& method_name, MethodParams *params) {
270: double_one_op(r, method_name, params, &exp);
271: }
272:
1.5 paf 273: static void _connect(Request& r, const String&, MethodParams *params) {
1.1 paf 274: Pool& pool=r.pool();
275:
1.5 paf 276: Value& url=params->get_no_junction(0, "url must not be code");
277: Value& body_code=params->get_junction(1, "body must be code");
1.1 paf 278:
1.19 parser 279: Table *protocol2driver_and_client=
280: static_cast<Table *>(r.classes_conf.get(OP->name()));
1.11 paf 281:
1.1 paf 282: // connect
283: SQL_Connection& connection=SQL_driver_manager->get_connection(
1.19 parser 284: url.as_string(), protocol2driver_and_client);
1.1 paf 285:
286: Exception rethrow_me;
287: // remember/set current connection
288: SQL_Connection *saved_connection=r.connection;
289: r.connection=&connection;
290: // execute body
291: bool body_failed=false;
292: PTRY
293: r.write_assign_lang(r.process(body_code));
294: PCATCH(e) { // connect/process problem
295: rethrow_me=e; body_failed=true;
296: }
297: PEND_CATCH
298:
299: bool finalizer_failed=false;
300: PTRY
301: // FINALLY
302: if(body_failed)
303: connection.rollback();
304: else
305: connection.commit();
306: PCATCH(e) { // commit/rollback problem
307: rethrow_me=e; finalizer_failed=true;
308: }
309: PEND_CATCH
310:
311: // close connection [cache it]
1.21 parser 312: connection.close();
1.1 paf 313: // recall current connection from remembered
314: r.connection=saved_connection;
315:
316: if(body_failed || finalizer_failed) // were there an exception for us to rethrow?
317: PTHROW(rethrow_me.type(), rethrow_me.code(),
318: rethrow_me.problem_source(),
319: rethrow_me.comment());
320: }
321:
1.28 parser 322: static String *switch_data_name;
323: static String *default_value;
324:
325: struct Switch_data {
326: Value *searching;
327: Value *found;
328: Value *_default;
329: };
330:
331: static void _switch(Request& r, const String&, MethodParams *params) {
332: void *backup=r.classes_conf.get(*switch_data_name);
333: Switch_data data={&r.process(params->get(0))};
334: r.classes_conf.put(*switch_data_name, &data);
335:
336: r.process(params->get_junction(1, "switch cases must be code")); // and ignore result
337:
338: r.classes_conf.put(*switch_data_name, backup);
339:
340: if(Value *code=data.found ? data.found : data._default)
341: r.write_pass_lang(r.process(*code));
342: }
343:
344: static void _case(Request& r, const String&, MethodParams *params) {
345: Switch_data& data=*static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
346:
347: int count=params->size();
348: Value *code=¶ms->get_junction(--count, "case result must be code");
349: for(int i=0; i<count; i++) {
350: Value& value=r.process(params->get(i));
351:
352: if(value.as_string() == *default_value) {
353: data._default=code;
354: break;
355: }
356:
357: bool matches;
358: if(data.searching->is_string())
359: matches=data.searching->as_string() == value.as_string();
360: else
361: matches=data.searching->as_double() == value.as_double();
362:
363: if(matches) {
364: data.found=code;
365: break;
366: }
367: }
368: }
369:
1.10 paf 370: // constructor
371:
1.11 paf 372: MOP::MOP(Pool& apool) : Methoded(apool),
373: main_sql_name(apool, MAIN_SQL_NAME),
374: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
375: {
1.10 paf 376: set_name(*NEW String(pool(), OP_CLASS_NAME));
1.1 paf 377:
1.28 parser 378: switch_data_name=NEW String(pool(), SWITCH_DATA_NAME);
379: default_value=NEW String(pool(), DEFAULT_VALUE);
380:
1.1 paf 381: // ^if(condition){code-when-true}
382: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 383: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 384:
385: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 386: add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
1.1 paf 387:
388: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 389: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 390:
391: // ^process[code]
1.10 paf 392: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 393:
394: // ^rem{code}
1.26 parser 395: add_native_method("rem", Method::CT_ANY, _rem, 1, 1000);
1.1 paf 396:
397: // ^while(condition){code}
1.10 paf 398: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 399:
400: // ^use[file]
1.10 paf 401: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 402:
403: // ^for[i;from-number;to-number-inclusive]{code}[delim]
1.10 paf 404: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 405:
406: // ^eval(expr)
407: // ^eval(expr)[format]
1.10 paf 408: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.1 paf 409:
410:
411: // math functions
412:
413: // ^round(expr)
1.10 paf 414: add_native_method("round", Method::CT_ANY, _round, 1, 1);
1.1 paf 415:
416: // ^floor(expr)
1.10 paf 417: add_native_method("floor", Method::CT_ANY, _floor, 1, 1);
1.1 paf 418:
419: // ^ceiling(expr)
1.10 paf 420: add_native_method("ceiling", Method::CT_ANY, _ceiling, 1, 1);
1.1 paf 421:
422: // ^abs(expr)
1.10 paf 423: add_native_method("abs", Method::CT_ANY, _abs, 1, 1);
1.1 paf 424:
425: // ^sign(expr)
1.10 paf 426: add_native_method("sign", Method::CT_ANY, _sign, 1, 1);
1.27 parser 427:
428: // ^log(expr)
429: add_native_method("log", Method::CT_ANY, _log, 1, 1);
430:
431: // ^exp(expr)
432: add_native_method("exp", Method::CT_ANY, _exp, 1, 1);
1.1 paf 433:
434:
435: // connect
436:
437: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 438: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
439:
1.28 parser 440: // switch
441:
442: // ^switch[value]{cases}
443: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
444:
445: // ^case[value]{code}
446: add_native_method("case", Method::CT_ANY, _case, 2, 1000);
1.10 paf 447: }
1.11 paf 448:
449: // constructor & configurator
1.1 paf 450:
1.10 paf 451: Methoded *MOP_create(Pool& pool) {
1.11 paf 452: return OP=new(pool) MOP(pool);
453: }
454:
455:
456: void MOP::configure_user(Request& r) {
457: Pool& pool=r.pool();
458:
459: // $MAIN:SQL.drivers
460: if(Value *sql=r.main_class->get_element(main_sql_name))
461: if(Value *element=sql->get_element(main_sql_drivers_name))
462: if(Table *protocol2library=element->get_table())
463: r.classes_conf.put(name(), protocol2library);
1.1 paf 464: }
E-mail: