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