Annotation of parser3/src/classes/op.C, revision 1.89
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
1.70 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.71 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
1.89 ! paf 7: $Id: op.C,v 1.88 2002/04/16 08:17:42 paf Exp $
1.1 paf 8: */
9:
1.13 paf 10: #include "classes.h"
1.1 paf 11: #include "pa_common.h"
12: #include "pa_request.h"
13: #include "pa_vint.h"
14: #include "pa_sql_connection.h"
1.79 paf 15: #include "pa_vdate.h"
1.1 paf 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.82 paf 24: #define CASE_DEFAULT_VALUE "DEFAULT"
1.11 paf 25:
1.10 paf 26: // class
27:
28: class MOP : public Methoded {
29: public:
30: MOP(Pool& pool);
1.11 paf 31: public: // Methoded
1.10 paf 32: bool used_directly() { return true; }
1.11 paf 33: void configure_user(Request& r);
1.36 parser 34:
1.11 paf 35: private:
36: String main_sql_name;
37: String main_sql_drivers_name;
1.10 paf 38: };
39:
40: // methods
41:
1.5 paf 42: static void _if(Request& r, const String&, MethodParams *params) {
1.48 parser 43: Value& condition_code=params->as_junction(0, "condition must be expression");
1.1 paf 44:
1.81 paf 45: bool condition=r.process_to_value(condition_code,
1.83 paf 46: /*0/*no name* /,*/
1.1 paf 47: false/*don't intercept string*/).as_bool();
1.6 paf 48: if(condition)
1.85 paf 49: r.write_pass_lang(r.process(params->as_junction(1, "'then' parameter must be code")));
1.50 parser 50: else if(params->size()>2)
1.85 paf 51: r.write_pass_lang(r.process(params->as_junction(2, "'else' parameter must be code")));
1.1 paf 52: }
53:
1.5 paf 54: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 55: Pool& pool=r.pool();
56:
1.60 paf 57: uchar lang;
1.59 paf 58: if(params->size()==1)
59: lang=String::UL_AS_IS; // mark as simply 'tainted'. useful in html from sql
60: else {
61: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 62: lang=untaint_lang_name2enum->get_int(lang_name);
1.59 paf 63: if(!lang)
1.78 paf 64: throw Exception(0,
1.59 paf 65: &lang_name,
66: "invalid taint language");
67: }
1.1 paf 68:
69: {
1.59 paf 70: Value& vbody=params->as_junction(params->size()-1, "body must be code");
1.1 paf 71:
72: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.86 paf 73: r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1 paf 74: }
75: }
76:
1.5 paf 77: static void _taint(Request& r, const String&, MethodParams *params) {
1.1 paf 78: Pool& pool=r.pool();
79:
1.60 paf 80: uchar lang;
1.1 paf 81: if(params->size()==1)
82: lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
83: else {
1.48 parser 84: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 85: lang=untaint_lang_name2enum->get_int(lang_name);
1.1 paf 86: if(!lang)
1.78 paf 87: throw Exception(0,
1.3 paf 88: &lang_name,
89: "invalid taint language");
1.1 paf 90: }
91:
92: {
1.34 parser 93: Value& vbody=params->as_no_junction(params->size()-1, "body must not be code");
1.1 paf 94:
95: String result(r.pool());
96: result.append(
97: vbody.as_string(), // process marking tainted with that lang
98: lang, true); // force result language to specified
99: r.write_pass_lang(result);
100: }
101: }
102:
1.5 paf 103: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 104: // calculate pseudo file name of processed chars
105: // would be something like "/some/file(4) process"
1.88 paf 106: char local_place[MAX_STRING];
1.1 paf 107: #ifndef NO_STRING_ORIGIN
108: const Origin& origin=method_name.origin();
1.88 paf 109: size_t place_size=snprintf(local_place, MAX_STRING, "%s(%d) %s",
1.87 paf 110: origin.file?origin.file:"unknown_file", 1+origin.line,
1.88 paf 111: method_name.cstr())+1;
1.1 paf 112: #else
1.88 paf 113: strncpy(local_place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
114: size_t place_size=strlen(local_place)+1;
115: #endif
116: char *heap_place=(char *)r.malloc(place_size);
117: memcpy(heap_place, local_place, place_size);
118:
1.1 paf 119:
120: VStateless_class& self_class=*r.self->get_class();
1.73 paf 121: const Method *main_method;
1.1 paf 122: {
1.73 paf 123: // temporary remove language change
124: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.1 paf 125: // temporary zero @main so to maybe-replace it in processed code
126: Temp_method temp_method_main(self_class, *main_method_name, 0);
127: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
128: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
129:
130: // evaluate source to process
1.86 paf 131: const String& source=r.process_to_string(params->as_junction(0, "body must be code"));
1.1 paf 132:
133: // process source code, append processed methods to 'self' class
134: // maybe-define new @main
1.67 paf 135: r.use_buf(
136: source.cstr(String::UL_UNSPECIFIED, r.connection(0)),
1.88 paf 137: heap_place,
1.67 paf 138: &self_class);
1.1 paf 139:
1.73 paf 140: // main_method
141: main_method=self_class.get_method(*main_method_name);
142: }
143: // after restoring current-request-lang
144: // maybe-execute @main[]
145: if(main_method) {
146: // execute!
147: r.execute(*main_method->parser_code);
1.1 paf 148: }
149: }
150:
1.5 paf 151: static void _rem(Request& r, const String&, MethodParams *params) {
1.34 parser 152: params->as_junction(0, "body must be code");
1.1 paf 153: }
154:
1.5 paf 155: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 156: Pool& pool=r.pool();
157:
1.34 parser 158: Value& vcondition=params->as_junction(0, "condition must be expression");
159: Value& body=params->as_junction(1, "body must be code");
1.1 paf 160:
161: // while...
162: int endless_loop_count=0;
163: while(true) {
1.18 parser 164: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.78 paf 165: throw Exception("parser.runtime",
1.1 paf 166: &method_name,
167: "endless loop detected");
168:
1.86 paf 169: bool condition=r.process_to_value(vcondition,
1.83 paf 170: /*0/*no name* /,*/
1.1 paf 171: false/*don't intercept string*/).as_bool();
172: if(!condition) // ...condition is true
173: break;
174:
175: // write processed body
1.86 paf 176: r.write_pass_lang(r.process(body));
1.1 paf 177: }
178: }
179:
1.5 paf 180: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 181: Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37 parser 182: r.use_file(vfile.as_string());
1.1 paf 183: }
184:
1.5 paf 185: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 186: Pool& pool=r.pool();
1.48 parser 187: const String& var_name=params->as_string(0, "var name must be string");
188: int from=params->as_int(1, "from must be int", r);
189: int to=params->as_int(2, "to must be int", r);
1.34 parser 190: Value& body_code=params->as_junction(3, "body must be code");
1.50 parser 191: Value *delim_maybe_code=params->size()>4?¶ms->get(4):0;
1.1 paf 192:
1.57 paf 193: if(to-from>=MAX_LOOPS) // too long loop?
1.78 paf 194: throw Exception("parser.runtime",
1.57 paf 195: &method_name,
196: "endless loop detected");
197:
1.1 paf 198: bool need_delim=false;
199: VInt *vint=new(pool) VInt(pool, 0);
200: for(int i=from; i<=to; i++) {
201: vint->set_int(i);
1.56 paf 202: r.root->put_element(var_name, vint);
1.1 paf 203:
1.86 paf 204: StringOrValue processed_body=r.process(body_code);
1.49 parser 205: if(delim_maybe_code) { // delimiter set?
1.1 paf 206: const String *string=processed_body.get_string();
207: if(need_delim && string && string->size()) // need delim & iteration produced string?
1.86 paf 208: r.write_pass_lang(r.process(*delim_maybe_code));
1.1 paf 209: need_delim=true;
210: }
211: r.write_pass_lang(processed_body);
212: }
213: }
214:
1.15 paf 215: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 216: Value& expr=params->as_junction(0, "need expression");
1.1 paf 217: // evaluate expresion
1.81 paf 218: Value *result=r.process_to_value(expr,
1.83 paf 219: /*0/*no name YET* /,*/
1.1 paf 220: true/*don't intercept string*/).as_expr_result();
1.50 parser 221: if(params->size()>1) {
1.34 parser 222: Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1 paf 223:
224: Pool& pool=r.pool();
225: String& string=*new(pool) String(pool);
226: string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
227: result=new(pool) VString(string);
228: }
1.16 paf 229: result->set_name(method_name);
1.1 paf 230: r.write_no_lang(*result);
231: }
232:
1.42 parser 233: static void _connect(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 234: Pool& pool=r.pool();
1.63 paf 235: #ifdef RESOURCES_DEBUG
236: struct timeval mt[2];
237: #endif
1.34 parser 238: Value& url=params->as_no_junction(0, "url must not be code");
239: Value& body_code=params->as_junction(1, "body must be code");
1.1 paf 240:
1.19 parser 241: Table *protocol2driver_and_client=
1.35 parser 242: static_cast<Table *>(r.classes_conf.get(r.OP.name()));
1.11 paf 243:
1.63 paf 244: #ifdef RESOURCES_DEBUG
245: //measure:before
246: gettimeofday(&mt[0],NULL);
247: #endif
1.1 paf 248: // connect
1.67 paf 249: SQL_Connection_ptr connection=SQL_driver_manager->get_connection(
1.42 parser 250: url.as_string(), method_name, protocol2driver_and_client);
1.1 paf 251:
1.63 paf 252: #ifdef RESOURCES_DEBUG
253: //measure:after connect
254: gettimeofday(&mt[1],NULL);
255:
256: double t[2];
257: for(int i=0;i<2;i++)
258: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
259:
260: r.sql_connect_time+=t[1]-t[0];
261: #endif
1.67 paf 262: Temp_connection temp_connection(r, connection.get());
1.1 paf 263: // execute body
1.53 parser 264: try {
1.86 paf 265: r.write_assign_lang(r.process(body_code));
1.75 paf 266: } catch(...) { // process problem
1.67 paf 267: connection->mark_to_rollback();
268: /*re*/throw;
1.1 paf 269: }
270: }
271:
1.41 parser 272: #ifndef DOXYGEN
1.28 parser 273: struct Switch_data {
1.82 paf 274: Request *r;
1.28 parser 275: Value *searching;
276: Value *found;
277: Value *_default;
278: };
1.41 parser 279: #endif
1.28 parser 280: static void _switch(Request& r, const String&, MethodParams *params) {
1.82 paf 281: Switch_data data={&r, &r.process_to_value(params->get(0))};
1.79 paf 282: Temp_hash_value switch_data_setter(r.classes_conf, *switch_data_name, &data);
1.28 parser 283:
1.83 paf 284: Value& cases_code=params->as_junction(1, "switch cases must be code");
1.82 paf 285: // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
286: // because of stacked WWrapper used there as wcontext
1.84 paf 287: r.process(cases_code, true/*intercept_string*/);
1.83 paf 288: if(Value *selected_code=data.found ? data.found : data._default) {
289: // setting code context, would execute in ^switch[...]{>>context<<}
290: selected_code->get_junction()->change_context(cases_code.get_junction());
1.86 paf 291: r.write_pass_lang(r.process(*selected_code));
1.83 paf 292: }
1.28 parser 293: }
294:
1.38 parser 295: static void _case(Request& r, const String& method_name, MethodParams *params) {
296: Pool& pool=r.pool();
297:
298: Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
299: if(!data)
1.78 paf 300: throw Exception("parser.runtime",
1.38 parser 301: &method_name,
302: "without switch");
1.28 parser 303:
304: int count=params->size();
1.34 parser 305: Value *code=¶ms->as_junction(--count, "case result must be code");
1.83 paf 306:
307: // killing context for safety, would execute in ^switch[...]{>>context<<}
308: // reason: context is stacked, and it would become invalid afterwards
309: code->get_junction()->change_context(0);
310:
1.28 parser 311: for(int i=0; i<count; i++) {
1.81 paf 312: Value& value=r.process_to_value(params->get(i));
1.28 parser 313:
1.82 paf 314: if(value.as_string() == CASE_DEFAULT_VALUE) {
1.38 parser 315: data->_default=code;
1.28 parser 316: break;
317: }
318:
319: bool matches;
1.38 parser 320: if(data->searching->is_string())
321: matches=data->searching->as_string() == value.as_string();
1.28 parser 322: else
1.38 parser 323: matches=data->searching->as_double() == value.as_double();
1.28 parser 324:
325: if(matches) {
1.82 paf 326: if(data->found)
327: throw Exception("parser.runtime",
328: &method_name,
329: "duplicate found");
330:
1.38 parser 331: data->found=code;
1.28 parser 332: break;
333: }
334: }
335: }
336:
1.63 paf 337: // cache--
338:
339: // consts
340:
1.79 paf 341: const int DATA_STRING_SERIALIZED_VERSION=0x0002;
1.63 paf 342:
343: // helper types
344:
345: #ifndef DOXYGEN
346: struct Data_string_serialized_prolog {
347: int version;
1.79 paf 348: time_t expires;
1.63 paf 349: };
350: #endif
351:
1.68 paf 352: void cache_delete(const String& file_spec) {
353: file_delete(file_spec, false/*fail_on_read_problem*/);
1.63 paf 354: }
1.69 paf 355:
356: #ifndef DOXYGEN
1.79 paf 357: struct Cache_data {
358: time_t expires;
359: };
1.69 paf 360: struct Locked_process_and_cache_put_action_info {
361: Request *r;
1.79 paf 362: Cache_data *data;
1.81 paf 363: Value *body_code; const String *evaluated_body;
1.69 paf 364: };
365: #endif
366: static void locked_process_and_cache_put_action(int f, void *context) {
367: Locked_process_and_cache_put_action_info& info=
368: *static_cast<Locked_process_and_cache_put_action_info *>(context);
369:
370: // body->process
1.81 paf 371: info.evaluated_body=&info.r->process_to_string(*info.body_code);
1.69 paf 372:
1.79 paf 373: // expiration time not spoiled by ^cache(0) or something?
374: if(info.data->expires > time(0)) {
375: // string -serialize> buffer
376: void *data; size_t data_size;
1.81 paf 377: info.evaluated_body->serialize(
1.79 paf 378: sizeof(Data_string_serialized_prolog),
379: data, data_size);
380: Data_string_serialized_prolog& prolog=
381: *static_cast<Data_string_serialized_prolog *>(data);
382: prolog.version=DATA_STRING_SERIALIZED_VERSION;
383: prolog.expires=info.data->expires;
384:
385: // buffer -write> file
386: write(f, data, data_size);
387: } else // expired!
388: info.data->expires=0; // flag it so that could be easily checked by caller
1.69 paf 389: }
1.81 paf 390: const String *locked_process_and_cache_put(Request& r,
1.69 paf 391: Value& body_code,
1.79 paf 392: Cache_data& data,
1.69 paf 393: const String& file_spec) {
394: Locked_process_and_cache_put_action_info info={
395: &r,
1.81 paf 396: &data,
397: &body_code
1.69 paf 398: };
399:
1.81 paf 400: const String *result=file_write_action_under_lock(
1.69 paf 401: file_spec,
402: "cache_put", locked_process_and_cache_put_action, &info,
403: false/*as_text*/,
404: false/*do_append*/,
1.81 paf 405: false/*block*/) ? info.evaluated_body: 0;
1.79 paf 406: if(data.expires==0)
407: cache_delete(file_spec);
408: return result;
1.63 paf 409: }
1.79 paf 410: String *cache_get(Pool& pool, const String& file_spec, time_t now) {
1.63 paf 411: void* data; size_t data_size;
1.72 paf 412: if(file_read(pool, file_spec,
1.63 paf 413: data, data_size,
414: false/*as_text*/,
1.69 paf 415: false/*fail_on_read_problem*/)
1.72 paf 416: && data_size/* ignore reads which are empty due to
417: non-unary open+lockEX conflict with lockSH */) {
1.63 paf 418:
1.72 paf 419: Data_string_serialized_prolog& prolog=
420: *static_cast<Data_string_serialized_prolog *>(data);
1.63 paf 421:
1.72 paf 422: String *result=new(pool) String(pool);
423: if(
424: data_size>=sizeof(Data_string_serialized_prolog)
425: && prolog.version==DATA_STRING_SERIALIZED_VERSION
1.79 paf 426: && prolog.expires > now
1.72 paf 427: && result->deserialize(
428: sizeof(Data_string_serialized_prolog), data, data_size, file_spec.cstr()))
429: return result;
430: }
1.63 paf 431:
1.72 paf 432: return 0;
1.63 paf 433: }
1.79 paf 434: static time_t as_expires(Request& r, const String& method_name, MethodParams *params,
435: int index, time_t now) {
436: time_t result;
437: Value& vlifespan_or_expires=params->get(index);
438: if(strcmp(vlifespan_or_expires.type(), VDATE_TYPE)==0)
439: result=static_cast<VDate&>(vlifespan_or_expires).get_time();
440: else
441: result=now+(time_t)params->as_double(index, "lifespan must be date or number", r);
442:
443: return result;
444: }
445: static const String as_file_spec(Request&r, MethodParams *params, int index) {
446: return r.absolute(params->as_string(index, "filespec must be string"));
447: }
1.63 paf 448: static void _cache(Request& r, const String& method_name, MethodParams *params) {
449: Pool& pool=r.pool();
1.79 paf 450: time_t now=time(0);
451:
452: // ^cache[filename] ^cache(seconds) ^cache[expires date]
453: if(params->size()==1) {
454: if(params->get(0).is_string()) { // filename?
455: cache_delete(as_file_spec(r, params, 0));
456: return;
457: }
458:
459: // secods|expires date
460: Cache_data *data=static_cast<Cache_data *>(r.classes_conf.get(*cache_data_name));
461: if(!data)
462: throw Exception("parser.runtime",
463: &method_name,
464: "expire-time reducing instruction without cache");
465:
466: time_t expires=as_expires(r, method_name, params, 0, now);
467: if(expires < data->expires)
468: data->expires=expires;
469:
470: return;
471: }
1.63 paf 472:
473: // file_spec, expires, body code
1.65 paf 474: const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
475:
1.79 paf 476: Cache_data data;
477: Temp_hash_value cache_data_setter(r.classes_conf, *cache_data_name, &data);
478: data.expires=as_expires(r, method_name, params, 1, now);
1.63 paf 479: Value& body_code=params->as_junction(2, "body must be code");
480:
1.79 paf 481: if(data.expires>now) { // valid 'expires' specified? try cached copy...
1.69 paf 482: // hence we don't hope to have unary create/lockEX
483: // we need some plan to live in a life like that, so...
484: // worst races plan:
485: // A B
486: // open
487: // |open
488: // lockSH
489: // |nonblocking-lockEX fails
490: // unlockSH
491: // close, cache_get returns 0
492: // open
493: // nonblocking-lockEX succeeds; process, write, close
494: // |retry1: open
495: // ...
496: // |lockSH succeeds; ...
497:
498: for(int retry=0; retry<2; retry++) {
1.79 paf 499: if(String *cached_body=cache_get(pool, file_spec, now)) { // have cached copy?
500: // write it out
501: r.write_assign_lang(*cached_body);
502: // happy with it
503: return;
504: }
1.69 paf 505:
506: // non-blocked lock; process; cache it
1.81 paf 507: if(const String*processed_body=
508: locked_process_and_cache_put(r, body_code, data, file_spec)) {
1.63 paf 509: // write it out
1.69 paf 510: r.write_assign_lang(*processed_body);
1.63 paf 511: // happy with it
512: return;
1.69 paf 513: } else { // somebody writing result right now
514: pa_sleep(0, 500000); // waiting half a second
515: retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63 paf 516: }
1.69 paf 517: }
1.78 paf 518: throw Exception(0,
1.69 paf 519: &file_spec,
520: "locking problem");
521: } else {
1.79 paf 522: // instructed not to cache; forget cached copy
1.68 paf 523: cache_delete(file_spec);
1.69 paf 524: // process
1.81 paf 525: const String& processed_body=r.process_to_string(body_code);
1.69 paf 526: // write it out
527: r.write_assign_lang(processed_body);
528: // happy with it
529: return;
530: }
531: // never reached
1.63 paf 532: }
533:
1.74 paf 534: // also used in pa_request.C to pass param to @unhandled_exception
535: VHash& exception2vhash(Pool& pool, const Exception& e) {
536: VHash& result=*new(pool) VHash(pool);
537: Hash& hash=result.hash(0);
1.78 paf 538: if(const char *type=e.type())
539: hash.put(*exception_type_part_name, new(pool) VString(*new(pool) String(pool, type)));
1.76 paf 540: if(const String *asource=e.problem_source()) {
541: String& source=*new(pool) String(pool);
542: source.append(*asource, String::UL_TAINTED, true/*forced*/);
543: result.set_name(source);
1.74 paf 544:
1.76 paf 545: hash.put(*exception_source_part_name, new(pool) VString(source));
1.74 paf 546: #ifndef NO_STRING_ORIGIN
1.76 paf 547: const Origin& origin=source.origin();
1.89 ! paf 548: hash.put(*new(pool) String(pool, "file", 0, true),
1.74 paf 549: new(pool) VString(*new(pool) String(pool, origin.file)));
550: hash.put(*new(pool) String(pool, "lineno"),
551: new(pool) VInt(pool, 1+origin.line));
552: #endif
553: }
554: if(const char *ecomment=e.comment()) {
555: int comment_size=strlen(ecomment);
556: char *pcomment=(char *)pool.malloc(comment_size);
557: memcpy(pcomment, ecomment, comment_size);
558: hash.put(*exception_comment_part_name,
1.76 paf 559: new(pool) VString(*new(pool) String(pool, pcomment, comment_size, true/*tainted*/)));
1.74 paf 560: }
561: hash.put(*exception_handled_part_name,
562: new(pool) VBool(pool, false));
563:
564: return result;
565: }
566:
567: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
568: Pool& pool=r.pool();
569:
570: Value& body_code=params->as_junction(0, "body_code must be code");
571: Value& catch_code=params->as_junction(1, "catch_code must be code");
572:
1.86 paf 573: StringOrValue result;
1.74 paf 574:
575: // taking snapshot of request processing status
1.77 paf 576: //int ssexception_trace=r.exception_trace.top_index();
577: int sstack=r.stack.top_index();
1.74 paf 578: Value *sself=r.self, *sroot=r.root, *srcontext=r.rcontext;
579: WContext *swcontext=r.wcontext;
580: try {
1.86 paf 581: result=r.process(body_code);
1.74 paf 582: } catch(const Exception& e) {
583: // restoring request processing status
1.77 paf 584: //r.exception_trace.top_index(ssexception_trace);
585: r.stack.top_index(sstack);
1.74 paf 586: r.self=sself; r.root=sroot, r.rcontext=srcontext; r.wcontext=swcontext;
1.77 paf 587:
1.74 paf 588:
589: VHash& vhash=exception2vhash(pool, e);
590:
591: Junction *junction=catch_code.get_junction();
592: Value *saved_exception_var_value=junction->root->get_element(*exception_var_name);
593: junction->root->put_element(*exception_var_name, &vhash);
1.86 paf 594: result=r.process(catch_code);
1.74 paf 595: bool handled=false;
596: if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
597: handled=value->as_bool();
598: junction->root->put_element(*exception_var_name, saved_exception_var_value);
599:
600: if(!handled)
601: throw(e); // rethrow
602: }
603: // write it out
1.86 paf 604: r.write_pass_lang(result);
1.74 paf 605: }
606:
607: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
608: Pool& pool=r.pool();
609:
610: if(params->size()==1) {
611: Value& param0=params->get(0);
612: if(Hash *hash=param0.get_hash(&method_name)) {
1.78 paf 613: const char *type=0;
1.74 paf 614: if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
1.78 paf 615: type=value->as_string().cstr();
1.74 paf 616: const String *source=0;
617: if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
618: source=&value->as_string();
619: const char *comment=0;
620: if(Value *value=
621: static_cast<Value *>(hash->get(*exception_comment_part_name)))
622: comment=value->as_string().cstr();
623:
1.78 paf 624: throw Exception(type,
1.74 paf 625: source?source:&method_name,
626: comment);
627: } else
1.78 paf 628: throw Exception("parser.runtime",
1.74 paf 629: &method_name,
630: "one-param version has hash param");
631: } else {
1.78 paf 632: const char *type=params->as_string(0, "type must be string").cstr();
1.74 paf 633: const String& source=params->as_string(1, "source must be string");
634: const char *comment=params->as_string(2, "comment must be string").cstr();
1.78 paf 635: throw Exception(type, &source, comment);
1.74 paf 636: }
637: }
638:
1.10 paf 639: // constructor
640:
1.11 paf 641: MOP::MOP(Pool& apool) : Methoded(apool),
642: main_sql_name(apool, MAIN_SQL_NAME),
643: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
644: {
1.10 paf 645: set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28 parser 646:
1.1 paf 647: // ^if(condition){code-when-true}
648: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 649: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 650:
651: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 652: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 653:
654: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 655: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 656:
657: // ^process[code]
1.10 paf 658: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 659:
660: // ^rem{code}
1.51 parser 661: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 662:
663: // ^while(condition){code}
1.10 paf 664: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 665:
666: // ^use[file]
1.10 paf 667: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 668:
1.54 paf 669: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 670: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 671:
672: // ^eval(expr)
673: // ^eval(expr)[format]
1.10 paf 674: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 675:
1.1 paf 676: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 677: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
678:
1.63 paf 679:
1.65 paf 680: // ^cache[file_spec] delete cache
1.63 paf 681: // ^cache[file_spec](time){code} time=0 no cache
1.65 paf 682: add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63 paf 683:
1.28 parser 684: // switch
685:
686: // ^switch[value]{cases}
687: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
688:
689: // ^case[value]{code}
1.51 parser 690: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74 paf 691:
692: // try-catch
693:
694: // ^try{code}{catch code}
695: add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
696: // ^throw[$exception hash]
697: // ^throw[type;source;comment]
698: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
699:
1.10 paf 700: }
1.11 paf 701:
702: // constructor & configurator
1.1 paf 703:
1.10 paf 704: Methoded *MOP_create(Pool& pool) {
1.35 parser 705: return new(pool) MOP(pool);
1.11 paf 706: }
707:
708: void MOP::configure_user(Request& r) {
709: Pool& pool=r.pool();
710:
711: // $MAIN:SQL.drivers
712: if(Value *sql=r.main_class->get_element(main_sql_name))
713: if(Value *element=sql->get_element(main_sql_drivers_name))
714: if(Table *protocol2library=element->get_table())
715: r.classes_conf.put(name(), protocol2library);
1.1 paf 716: }
E-mail: