|
|
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.35 ! paf 6: $Id: pa_request.C,v 1.34 2001/03/14 17:09:18 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*/,
72: main_class_name);
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);
79: VStateless_class *main_class=use_file(
80: auto_filespec, false/*ignore possible read problem*/,
81: main_class_name);
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;
90: size_t value=element?(size_t)element->get_double():0;
91: size_t post_max_size=value?value:10*0x400*400;
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]
131: VMethodFrame *frame=NEW VMethodFrame(pool(), *junction);
132:
133: const String *problem_source=e.problem_source();
134: // origin
135: LOCAL_STRING(origin_name, "origin");
136: VString *origin_value=0;
137: #ifndef NO_STRING_ORIGIN
138: const Origin& origin=problem_source->origin();
139: if(origin.file) {
140: char *buf=(char *)malloc(MAX_STRING);
141: snprintf(buf, MAX_STRING, "%s(%d): ",
142: origin.file, 1+origin.line);
143: String *NEW_STRING(origin_file_line, buf);
144: origin_value=NEW VString(*origin_file_line);
145: }
146: #endif
147: frame->store_param(origin_name, origin_value);
1.28 paf 148:
1.30 paf 149: // source
150: LOCAL_STRING(source_name, "source");
151: frame->store_param(source_name,
152: NEW VString(*problem_source));
153:
154: // comment
155: LOCAL_STRING(comment_name, "comment");
156: String *NEW_STRING(comment_value, e.comment());
157: frame->store_param(comment_name,
158: NEW VString(*comment_value));
159:
160: // type
161: LOCAL_STRING(type_name, "type");
162: Value *type_value;
163: if(e.type())
164: type_value=NEW VString(*e.type());
165: else
166: type_value=NEW VUnknown(pool());
167: frame->store_param(type_name, type_value);
168:
169: // code
170: LOCAL_STRING(code_name, "code");
171: Value *code_value;
172: if(e.code())
173: code_value=NEW VString(*e.code());
174: else
175: code_value=NEW VUnknown(pool());
176: frame->store_param(code_name, code_value);
177:
178: result=execute_method(*frame, *method);
179: }
180: }
181:
182: // couldn't report an error beautifully, doing that ugly
183: if(!result) {
184: result=(char *)malloc(MAX_STRING);
185: result[0]=0;
186: size_t printed=0;
187: const String *problem_source=e.problem_source();
188: if(problem_source) {
1.16 paf 189: #ifndef NO_STRING_ORIGIN
1.30 paf 190: const Origin& origin=problem_source->origin();
191: if(origin.file)
192: printed+=snprintf(result+printed, MAX_STRING-printed, "%s(%d): ",
193: origin.file, 1+origin.line);
1.28 paf 194: #endif
1.30 paf 195: printed+=snprintf(result+printed, MAX_STRING-printed, "'%s' ",
196: problem_source->cstr());
197: }
198: printed+=snprintf(result+printed, MAX_STRING-printed, "%s",
199: e.comment());
200: const String *type=e.type();
201: if(type) {
202: printed+=snprintf(result+printed, MAX_STRING-printed, " type: %s",
203: type->cstr());
204: const String *code=e.code();
205: if(code)
206: printed+=snprintf(result+printed, MAX_STRING-printed, ", code: %s",
207: code->cstr());
208: }
209: }
1.3 paf 210: }
1.30 paf 211: CATCH(e) {
212: // exception in exception handler occured
213: // probably totally out of memory
214: // can't say anything about such sad story
215: // would just return 0
216: result=0;
1.1 paf 217: }
1.30 paf 218: END_CATCH
1.1 paf 219: }
1.3 paf 220: END_CATCH
1.28 paf 221:
222: return result;
1.1 paf 223: }
224:
1.26 paf 225: VStateless_class *Request::use_file(
226: const char *file, bool fail_on_read_problem,
227: const String *name,
228: VStateless_class *base_class) {
1.3 paf 229: // TODO: обнаружить|решить cyclic dependences
1.5 paf 230: char *source=file_read(pool(), file, fail_on_read_problem);
1.3 paf 231: if(!source)
1.12 paf 232: return base_class;
1.3 paf 233:
1.25 paf 234: return use_buf(source, file, 0/*new class*/, name, base_class);
1.16 paf 235: }
236:
1.26 paf 237: VStateless_class *Request::use_buf(
238: const char *source, const char *file,
239: VStateless_class *aclass, const String *name,
240: VStateless_class *base_class) {
1.5 paf 241: // compile loaded class
1.26 paf 242: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 243:
1.4 paf 244: // locate and execute possible @auto[] static method
1.25 paf 245: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 246: return &cclass;
1.20 paf 247: }
248:
249: void Request::fail_if_junction_(bool is,
1.26 paf 250: Value& value, const String& method_name, char *msg) {
1.20 paf 251:
252: // fail_if_junction(true, junction = fail
253: // fail_if_junction(false, not junction = fail
254: if((value.get_junction()!=0) ^ !is)
255: THROW(0, 0,
256: &method_name,
257: msg);
1.19 paf 258: }
259:
260: char *Request::relative(const char *path, const char *file) {
1.21 paf 261: char *result=(char *)malloc(strlen(path)+strlen(file)+1);
1.19 paf 262: strcpy(result, path);
1.32 paf 263: rsplit(result, PATH_DELIMITER_CHAR);
264: strcat(result, PATH_DELIMITER_STRING);
1.19 paf 265: strcat(result, file);
266: return result;
267: }
268:
269: char *Request::absolute(const char *name) {
270: if(name[0]=='/') {
1.35 ! paf 271: char *result=(char *)malloc(strlen(info.document_root)+strlen(name)+1);
! 272: strcpy(result, info.document_root);
1.19 paf 273: strcat(result, name);
274: return result;
275: } else
1.35 ! paf 276: return relative(info.request_uri, name);
1.1 paf 277: }