|
|
1.1 paf 1: /*
1.9 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.14 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.9 paf 5:
1.38 ! paf 6: $Id: pa_request.C,v 1.37 2001/03/15 09:37:56 paf Exp $
1.1 paf 7: */
8:
1.19 paf 9: #include <string.h>
10:
1.1 paf 11: #include "pa_request.h"
1.3 paf 12: #include "pa_wwrapper.h"
13: #include "pa_common.h"
14: #include "pa_vclass.h"
1.11 paf 15: #include "_root.h"
16: #include "_env.h"
1.17 paf 17: #include "_table.h"
1.31 paf 18: #include "pa_globals.h"
1.30 paf 19: #include "pa_vint.h"
20: #include "pa_vmframe.h"
1.32 paf 21: #include "pa_types.h"
1.3 paf 22:
1.30 paf 23: #define NEW_STRING(name, value) name=NEW String(pool()); name->APPEND_CONST(value)
24: #define LOCAL_STRING(name, value) String name(pool()); name.APPEND_CONST(value)
1.3 paf 25:
1.28 paf 26: Request::Request(Pool& apool,
1.33 paf 27: Info& ainfo,
28: String::Untaint_lang alang) : Pooled(apool),
1.3 paf 29: stack(apool),
30: root_class(apool),
31: env_class(apool),
1.27 paf 32: form_class(apool),
1.3 paf 33: fclasses(apool),
1.28 paf 34: flang(alang),
1.33 paf 35: info(ainfo)
1.3 paf 36: {
1.27 paf 37: // root superclass,
1.3 paf 38: // parent of all classes,
39: // operators holder
1.27 paf 40: initialize_root_class(pool(), root_class);
1.6 paf 41: classes().put(*root_class_name, &root_class);
1.27 paf 42: // table class
43: classes().put(*table_class_name, table_class);
44: // table_class->set_name(*table_class_name);
1.3 paf 45:
46: // env class
1.6 paf 47: classes().put(*env_class_name, &env_class);
1.27 paf 48: // form class
49: classes().put(*form_class_name, &form_class);
1.3 paf 50: }
1.1 paf 51:
1.29 paf 52: char *Request::core(const char *sys_auto_path1,
53: const char *sys_auto_path2) {
1.28 paf 54: char *result;
1.29 paf 55: VStateless_class *main_class=0;
1.3 paf 56: TRY {
1.29 paf 57: char *auto_filespec=(char *)malloc(MAX_STRING);
58:
59: // load MAIN class,
60: // it consists of all the auto.p files we'd manage to find
61: // plus
62: // the file user requested us to process
63: // all located classes become children of one another,
64: // composing class we name 'MAIN'
65:
66: // loading system auto.p 1
67: if(sys_auto_path1) {
1.30 paf 68: strncpy(auto_filespec, sys_auto_path1, MAX_STRING-strlen(AUTO_FILE_NAME));
1.29 paf 69: strcat(auto_filespec, AUTO_FILE_NAME);
70: main_class=use_file(
71: auto_filespec, false/*ignore possible read problem*/,
1.37 paf 72: main_class_name, main_class);
1.29 paf 73: }
74:
75: // loading system auto.p 2
76: if(sys_auto_path2) {
1.30 paf 77: strncpy(auto_filespec, sys_auto_path2, MAX_STRING-strlen(AUTO_FILE_NAME));
1.29 paf 78: strcat(auto_filespec, AUTO_FILE_NAME);
1.37 paf 79: main_class=use_file(
1.29 paf 80: auto_filespec, false/*ignore possible read problem*/,
1.37 paf 81: main_class_name, main_class);
1.29 paf 82: }
1.8 paf 83:
1.34 paf 84: // $MAIN:limits hash used here,
85: // until someone with less privileges have overriden them
86: Value *limits=main_class?main_class->get_element(*limits_name):0;
87: Value *element;
1.35 paf 88: // $limits.post_max_size default 10M
1.34 paf 89: element=limits?limits->get_element(*post_max_size_name):0;
1.36 paf 90: int value=element?(size_t)element->get_double():0;
91: int post_max_size=value?value:10*0x400*400;
1.34 paf 92:
93: form_class.fill_fields(*this, post_max_size);
1.7 paf 94:
95: // TODO: load site auto.p files, all assigned bases from upper dir
96: char *site_auto_file="Y:\\parser3\\src\\auto.p";
1.16 paf 97: main_class=use_file(
98: site_auto_file, false/*ignore possible read problem*/,
1.25 paf 99: main_class_name, main_class);
1.7 paf 100:
101: // there must be some auto.p
1.12 paf 102: if(!main_class)
1.7 paf 103: THROW(0,0,
104: 0,
1.24 paf 105: "no 'auto.p' found (nither system nor any site's)");
1.5 paf 106:
1.7 paf 107: // compiling requested file
1.35 paf 108: main_class=use_file(info.path_translated, true/*don't ignore read problem*/,
1.25 paf 109: main_class_name, main_class);
110:
111: // execute @main[]
1.30 paf 112: result=execute_method(*main_class, *main_method_name);
1.25 paf 113: if(!result)
114: THROW(0,0,
115: 0,
116: "'"MAIN_METHOD_NAME"' method not found");
1.3 paf 117: }
118: CATCH(e) {
1.30 paf 119: TRY {
120: // we're returning not result, but error explanation
121: result=0;
122:
123: if(main_class) { // we've managed to end up with some main_class
124: // maybe we'd be lucky enough as to report an error
125: // in a gracefull way...
126: if(Value *value=main_class->get_element(*exception_method_name))
127: if(Junction *junction=value->get_junction())
128: if(const Method *method=junction->method) {
129: // preparing to pass parameters to
130: // @exception[origin;source;comment;type;code]
1.38 ! paf 131: VMethodFrame frame(pool(), *junction);
! 132: frame.set_self(*main_class);
1.30 paf 133:
134: const String *problem_source=e.problem_source();
135: // origin
136: LOCAL_STRING(origin_name, "origin");
1.38 ! paf 137: Value *origin_value=0;
1.30 paf 138: #ifndef NO_STRING_ORIGIN
1.38 ! paf 139: if(problem_source) {
! 140: const Origin& origin=problem_source->origin();
! 141: if(origin.file) {
! 142: char *buf=(char *)malloc(MAX_STRING);
! 143: snprintf(buf, MAX_STRING, "%s(%d): ",
! 144: origin.file, 1+origin.line);
! 145: String *NEW_STRING(origin_file_line, buf);
! 146: origin_value=NEW VString(*origin_file_line);
! 147: }
1.30 paf 148: }
149: #endif
1.38 ! paf 150: frame.store_param(origin_name,
! 151: origin_value?origin_value:NEW VUnknown(pool()));
1.28 paf 152:
1.30 paf 153: // source
154: LOCAL_STRING(source_name, "source");
1.38 ! paf 155: Value *source_value=0;
! 156: if(problem_source)
! 157: source_value=NEW VString(*problem_source);
! 158: frame.store_param(source_name,
! 159: source_value?source_value:NEW VUnknown(pool()));
1.30 paf 160:
161: // comment
162: LOCAL_STRING(comment_name, "comment");
163: String *NEW_STRING(comment_value, e.comment());
1.38 ! paf 164: frame.store_param(comment_name,
1.30 paf 165: NEW VString(*comment_value));
166:
167: // type
168: LOCAL_STRING(type_name, "type");
169: Value *type_value;
170: if(e.type())
171: type_value=NEW VString(*e.type());
172: else
173: type_value=NEW VUnknown(pool());
1.38 ! paf 174: frame.store_param(type_name, type_value);
1.30 paf 175:
176: // code
177: LOCAL_STRING(code_name, "code");
178: Value *code_value;
179: if(e.code())
180: code_value=NEW VString(*e.code());
181: else
182: code_value=NEW VUnknown(pool());
1.38 ! paf 183: frame.store_param(code_name, code_value);
1.30 paf 184:
1.38 ! paf 185: result=execute_method(frame, *method);
1.30 paf 186: }
187: }
188:
189: // couldn't report an error beautifully, doing that ugly
190: if(!result) {
191: result=(char *)malloc(MAX_STRING);
192: result[0]=0;
193: size_t printed=0;
194: const String *problem_source=e.problem_source();
195: if(problem_source) {
1.16 paf 196: #ifndef NO_STRING_ORIGIN
1.30 paf 197: const Origin& origin=problem_source->origin();
198: if(origin.file)
199: printed+=snprintf(result+printed, MAX_STRING-printed, "%s(%d): ",
200: origin.file, 1+origin.line);
1.28 paf 201: #endif
1.30 paf 202: printed+=snprintf(result+printed, MAX_STRING-printed, "'%s' ",
203: problem_source->cstr());
204: }
205: printed+=snprintf(result+printed, MAX_STRING-printed, "%s",
206: e.comment());
207: const String *type=e.type();
208: if(type) {
209: printed+=snprintf(result+printed, MAX_STRING-printed, " type: %s",
210: type->cstr());
211: const String *code=e.code();
212: if(code)
213: printed+=snprintf(result+printed, MAX_STRING-printed, ", code: %s",
214: code->cstr());
215: }
216: }
1.3 paf 217: }
1.30 paf 218: CATCH(e) {
219: // exception in exception handler occured
220: // probably totally out of memory
221: // can't say anything about such sad story
222: // would just return 0
223: result=0;
1.1 paf 224: }
1.30 paf 225: END_CATCH
1.1 paf 226: }
1.3 paf 227: END_CATCH
1.28 paf 228:
229: return result;
1.1 paf 230: }
231:
1.26 paf 232: VStateless_class *Request::use_file(
233: const char *file, bool fail_on_read_problem,
234: const String *name,
235: VStateless_class *base_class) {
1.3 paf 236: // TODO: обнаружить|решить cyclic dependences
1.5 paf 237: char *source=file_read(pool(), file, fail_on_read_problem);
1.3 paf 238: if(!source)
1.12 paf 239: return base_class;
1.3 paf 240:
1.25 paf 241: return use_buf(source, file, 0/*new class*/, name, base_class);
1.16 paf 242: }
243:
1.26 paf 244: VStateless_class *Request::use_buf(
245: const char *source, const char *file,
246: VStateless_class *aclass, const String *name,
247: VStateless_class *base_class) {
1.5 paf 248: // compile loaded class
1.26 paf 249: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 250:
1.4 paf 251: // locate and execute possible @auto[] static method
1.25 paf 252: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 253: return &cclass;
1.20 paf 254: }
255:
256: void Request::fail_if_junction_(bool is,
1.26 paf 257: Value& value, const String& method_name, char *msg) {
1.20 paf 258:
259: // fail_if_junction(true, junction = fail
260: // fail_if_junction(false, not junction = fail
261: if((value.get_junction()!=0) ^ !is)
262: THROW(0, 0,
263: &method_name,
264: msg);
1.19 paf 265: }
266:
267: char *Request::relative(const char *path, const char *file) {
1.21 paf 268: char *result=(char *)malloc(strlen(path)+strlen(file)+1);
1.19 paf 269: strcpy(result, path);
1.32 paf 270: rsplit(result, PATH_DELIMITER_CHAR);
271: strcat(result, PATH_DELIMITER_STRING);
1.19 paf 272: strcat(result, file);
273: return result;
274: }
275:
276: char *Request::absolute(const char *name) {
277: if(name[0]=='/') {
1.35 paf 278: char *result=(char *)malloc(strlen(info.document_root)+strlen(name)+1);
279: strcpy(result, info.document_root);
1.19 paf 280: strcat(result, name);
281: return result;
282: } else
1.35 paf 283: return relative(info.request_uri, name);
1.1 paf 284: }