Annotation of parser3/src/main/pa_request.C, revision 1.40

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

E-mail: