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

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

E-mail: