Annotation of parser3/src/main/pa_common.C, revision 1.143.2.12

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly functions.
                      3: 
1.143.2.10  paf         4:        Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.101     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111     paf         6: */
1.16      paf         7: 
1.143.2.12! paf         8: static const char* IDENT_COMMON_C="$Date: 2003/01/31 14:03:54 $"; 
1.1       paf         9: 
1.143.2.12! paf        10: //#include "pa_value_includes.h"
1.1       paf        11: #include "pa_common.h"
1.4       paf        12: #include "pa_exception.h"
1.143.2.8  paf        13: #include "pa_value_includes.h"
1.126     paf        14: #include "pa_hash.h"
1.143.2.3  paf        15: #include "pa_vstring.h"
1.143.2.4  paf        16: #include "pa_vint.h"
1.1       paf        17: 
1.143.2.12! paf        18: #ifdef CYGWIN
        !            19: #define _GNU_H_WINDOWS32_SOCKETS
        !            20: // for PASCAL
        !            21: #include <windows.h>
        !            22: // SOCKET
        !            23: typedef u_int  SOCKET;
        !            24: int PASCAL closesocket(SOCKET);
1.126     paf        25: #else
1.143.2.12! paf        26: #      if defined(WIN32)
        !            27: #              include <windows.h>
        !            28: #      else
        !            29: #              define closesocket close
        !            30: #      endif
1.98      paf        31: #endif
                     32: 
1.93      paf        33: // some maybe-undefined constants
                     34: 
1.82      paf        35: #ifndef _O_TEXT
                     36: #      define _O_TEXT 0
                     37: #endif
                     38: #ifndef _O_BINARY
                     39: #      define _O_BINARY 0
1.47      paf        40: #endif
1.80      paf        41: 
1.138     paf        42: #ifdef HAVE_FTRUNCATE
                     43: #      define PA_O_TRUNC 0
                     44: #else
                     45: #      ifdef _O_TRUNC
                     46: #              define PA_O_TRUNC _O_TRUNC
                     47: #      else
                     48: #              error you must have either ftruncate function or _O_TRUNC bit declared
                     49: #      endif
                     50: #endif
                     51: 
1.93      paf        52: // locking constants
                     53: 
1.99      paf        54: #ifdef HAVE_FLOCK
                     55: 
                     56: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
                     57: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
                     58: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
                     59: static int unlock(int fd) { return flock(fd, LOCK_UN); }
                     60: 
1.98      paf        61: #else
1.99      paf        62: #ifdef HAVE__LOCKING
1.98      paf        63: 
1.126     paf        64: #define FLOCK(operation) lseek(fd, 0, SEEK_SET);  return _locking(fd, operation, 1)
1.99      paf        65: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
                     66: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
                     67: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
                     68: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93      paf        69: 
1.99      paf        70: #else
                     71: #ifdef HAVE_FCNTL
1.93      paf        72: 
1.126     paf        73: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET};  return fcntl(fd, cmd, &ls)
1.99      paf        74: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
                     75: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
                     76: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
                     77: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93      paf        78: 
                     79: #else
                     80: #ifdef HAVE_LOCKF
1.99      paf        81: 
1.126     paf        82: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET);  return lockf(fd, operation, 1)
1.99      paf        83: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
                     84: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
                     85: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
                     86: static int unlock(int fd) { FLOCK(F_TLOCK); }
                     87: 
1.93      paf        88: #else
1.99      paf        89: 
                     90: #error unable to find file locking func
                     91: 
                     92: #endif
1.93      paf        93: #endif
                     94: #endif
                     95: #endif
                     96: 
1.127     paf        97: #define DEFAULT_USER_AGENT "parser3"
                     98: 
                     99: 
1.126     paf       100: void fix_line_breaks(char* buf, size_t& size) {
1.139     paf       101:        if(size==0)
                    102:                return;
                    103: 
1.87      paf       104:        //_asm int 3;
1.126     paf       105:        const char* const eob=buf+size;
                    106:        char* dest=buf;
1.72      parser    107:        // fix DOS: \r\n -> \n
                    108:        // fix Macintosh: \r -> \n
1.126     paf       109:        char* bol=buf;
1.137     paf       110:        while(char* eol=(char*)memchr(bol, '\r', eob -bol)) {
1.72      parser    111:                size_t len=eol-bol;
                    112:                if(dest!=bol)
1.126     paf       113:                        memcpy(dest, bol, len); 
1.72      parser    114:                dest+=len;
1.126     paf       115:                *dest++='\n'; 
1.72      parser    116: 
1.126     paf       117:                if(&eol[1]<eob && eol[1]=='\n') { // \r, \n = DOS
1.72      parser    118:                        bol=eol+2;
1.126     paf       119:                        size--; 
                    120:                } else // \r, not \n = Macintosh
1.72      parser    121:                        bol=eol+1;
                    122:        }
                    123:        // last piece without \r, including terminating 0
                    124:        if(dest!=bol)
1.126     paf       125:                memcpy(dest, bol, eob-bol); 
1.72      parser    126: }
1.18      paf       127: 
1.143.2.3  paf       128: char* file_read_text(Pool& pool, Charset& charset, 
1.143.2.9  paf       129:                                         StringPtr file_spec, 
1.126     paf       130:                                         bool fail_on_read_problem,
1.143.2.3  paf       131:                                         HashStringValue *params, HashStringValue** out_fields) {
1.143.2.4  paf       132:        char *result;  size_t size;
                    133:        File_read_result file_read_result=file_read(pool, charset, file_spec, result, size, true, params, out_fields, fail_on_read_problem);
                    134:        return file_read_result.success?result:0;
1.126     paf       135: }
                    136: 
                    137: //http request stuff
                    138: /* ************************ http stuff *********************** */
                    139: 
                    140: static bool set_addr(struct sockaddr_in *addr, const char* host, const short port){
                    141:     memset(addr, 0, sizeof(*addr)); 
                    142:     addr->sin_family=AF_INET;
                    143:     addr->sin_port=htons(port); 
                    144:     if(host) {
                    145:                if(struct hostent *hostIP=gethostbyname(host)) 
                    146:                        memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); 
                    147:                else
                    148:                        return false;
                    149:     } else 
                    150:                addr->sin_addr.s_addr=INADDR_ANY;
                    151:     return true;
                    152: }
                    153: 
1.143.2.1  paf       154: static int http_read_response(Pool& pool, String& response, int sock, bool fail_on_status_ne_200){
1.143.2.2  paf       155:        int result=0;
1.130     paf       156:        ssize_t EOLat=0;
1.126     paf       157:        while(true) {
1.143.2.1  paf       158:                char *buf=(char *)pool.malloc(MAX_STRING); 
1.126     paf       159:                ssize_t size=recv(sock, buf, MAX_STRING, 0); 
                    160:                if(size<=0)
                    161:                        break;
1.130     paf       162:                response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); 
1.143.2.2  paf       163:                if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response
1.143.2.9  paf       164:                        StringPtr status_line=response.mid(0, (size_t)EOLat);
                    165:                        Array<StringPtr> astatus; 
1.143.2.3  paf       166:                        status_line->split(astatus, 0, " ", 1); 
1.143.2.9  paf       167:                        StringPtr status_code=astatus.get(1);
1.143.2.3  paf       168:                        result=status_code->as_int(); 
1.142     paf       169: 
1.143.2.3  paf       170:                        if(fail_on_status_ne_200 && result!=200)
1.142     paf       171:                                throw Exception("http.status",
                    172:                                        status_code,
                    173:                                        "invalid HTTP response status");
                    174:                }
                    175:        }
1.143.2.2  paf       176:        if(result)
                    177:                return result;
1.142     paf       178:        else
                    179:                throw Exception("http.response",
1.143.2.3  paf       180:                        Exception::undefined_source,
1.142     paf       181:                        "bad response from host - no status found (size=%lu)", response.size()); 
1.126     paf       182: }
                    183: 
                    184: /* ********************** request *************************** */
                    185: 
                    186: #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP)
                    187: #      define WE_CAN_USE_ALARM
                    188: #endif
                    189: 
                    190: #ifdef WE_CAN_USE_ALARM
                    191: static sigjmp_buf timeout_env;
                    192: static void timeout_handler(int sig){
                    193:     siglongjmp(timeout_env, 1); 
                    194: }
                    195: #endif
                    196: 
1.143.2.3  paf       197: static int http_request(Pool& pool,
                    198:                                                String& response,
1.143.2.9  paf       199:                                                StringPtr origin_string, 
1.143.2.3  paf       200:                                                const char* host, int port, 
                    201:                                                const char* request, 
                    202:                                                int timeout,
                    203:                                                bool fail_on_status_ne_200) {
1.126     paf       204:        if(!host)
                    205:                throw Exception("http.host", 
                    206:                        origin_string, 
                    207:                        "zero hostname");  //never
                    208: 
                    209: #ifdef WE_CAN_USE_ALARM
                    210:     signal(SIGALRM, timeout_handler); 
                    211: #endif
                    212:        int sock=-1;
                    213:        try {
1.142     paf       214:                int result;
1.126     paf       215: #ifdef WE_CAN_USE_ALARM
                    216:                if(sigsetjmp(timeout_env, 1))
                    217:                        throw Exception("http.timeout", 
                    218:                                origin_string, 
                    219:                                "timeout occured while retrieving document"); 
                    220:                else {
                    221:                        alarm(timeout); 
                    222: #endif
                    223:                        struct sockaddr_in dest;
                    224:                
                    225:                if(!set_addr(&dest, host, port))
                    226:                                throw Exception("http.host", 
                    227:                                        origin_string, 
1.127     paf       228:                                        "can not resolve hostname \"%s\"", host); 
1.126     paf       229:                        
                    230:                        if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0)
                    231:                                throw Exception("http.connect", 
                    232:                                        origin_string, 
1.127     paf       233:                                        "can not make socket: %s (%d)", strerror(errno), errno); 
1.126     paf       234:                        if(connect(sock, (struct sockaddr *)&dest, sizeof(dest)))
                    235:                                throw Exception("http.connect", 
                    236:                                        origin_string, 
1.127     paf       237:                                        "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); 
1.126     paf       238:                        size_t request_size=strlen(request);
                    239:                        if(send(sock, request, request_size, 0)!=(ssize_t)request_size)
                    240:                                throw Exception("http.connect", 
                    241:                                        origin_string, 
1.127     paf       242:                                        "error sending request: %s (%d)", strerror(errno), errno); 
1.126     paf       243: 
1.143.2.3  paf       244:                        result=http_read_response(pool, response, sock, fail_on_status_ne_200); 
1.142     paf       245:                        closesocket(sock); 
1.126     paf       246: #ifdef WE_CAN_USE_ALARM
1.142     paf       247:                        alarm(0)1.126     paf       248:                }
                    249: #endif
1.142     paf       250:                return result;
1.126     paf       251:        } catch(...) {
                    252:                if(sock>=0) 
                    253:                        closesocket(sock); 
                    254: #ifdef WE_CAN_USE_ALARM
                    255:                alarm(0); 
                    256: #endif
1.143.2.11  paf       257:                rethrow;
1.126     paf       258:        }
                    259: }
                    260: 
1.127     paf       261: #ifndef DOXYGEN
                    262: struct Http_pass_header_info {
1.143.2.3  paf       263:        Pool* pool;
                    264:        Charset* charset;
1.127     paf       265:        String* request;
                    266:        bool user_agent_specified;
                    267: };
                    268: #endif
1.143.2.3  paf       269: static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, 
                    270:                                                         Http_pass_header_info *info) {
                    271:     *(info->request)<<*key<<": "
1.143.2.9  paf       272:                << *attributed_meaning_to_string(*info->pool, value, String::UL_HTTP_HEADER, false)
1.135     paf       273:                <<"\n"; 
                    274:        
1.143.2.3  paf       275:        if(*key->change_case(*info->pool, *info->charset, String::CC_UPPER)=="USER-AGENT")
                    276:                info->user_agent_specified=true;
1.126     paf       277: }
1.143.2.9  paf       278: static HashStringValuePtr file_read_http(Pool& pool, Charset& charset, StringPtr file_spec, 
1.143.2.3  paf       279:                                        char*& data, size_t& data_size, 
                    280:                                        HashStringValue *options=0, bool return_outfields=false) {
                    281:        HashStringValuePtr result(0);
1.126     paf       282:        char host[MAX_STRING]; 
1.129     paf       283:        const char* uri; 
1.126     paf       284:        int port;
1.143.2.5  paf       285:        CharPtr method(0);
1.126     paf       286:        int timeout=2;
1.142     paf       287:        bool fail_on_status_ne_200=true;
1.143.2.3  paf       288:        ValuePtr vheaders(0);
1.126     paf       289: 
1.143.2.3  paf       290:        StringPtr connect_string(new String());
1.133     paf       291:        // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one
1.143.2.4  paf       292:        connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces
1.133     paf       293: 
1.143.2.3  paf       294:        CharPtr connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED); 
1.143.2.10  paf       295:        const char* current=connect_string_cstr;
1.143.2.3  paf       296:        if(strncmp(current, "http://", 7)!=0)
1.126     paf       297:                throw Exception(0, 
1.143.2.3  paf       298:                        connect_string, 
1.126     paf       299:                        "does not start with http://"); //never
1.143.2.3  paf       300:        current+=7;
1.126     paf       301: 
1.143.2.3  paf       302:        strncpy(host, current, sizeof(host)-1);  host[sizeof(host)-1]=0;
1.126     paf       303:        char* host_uri=lsplit(host, '/'); 
1.143.2.3  paf       304:        uri=host_uri?current+(host_uri-1-host):"/"; 
1.126     paf       305:        char* port_cstr=lsplit(host, ':'); 
                    306:        char* error_pos=0;
                    307:        port=port_cstr?strtol(port_cstr, &error_pos, 0):80;
                    308: 
1.127     paf       309:        if(options) {
                    310:                int valid_options=0;
1.143.2.3  paf       311:                if(ValuePtr vmethod=options->get(http_method_name)) {
1.127     paf       312:                        valid_options++;
1.143.2.8  paf       313:                        method=vmethod->as_string(&pool)->cstr();
1.127     paf       314:                }
1.143.2.3  paf       315:                if(ValuePtr vtimeout=options->get(http_timeout_name)) {
1.127     paf       316:                        valid_options++;
                    317:                        timeout=vtimeout->as_int(); 
                    318:                }
1.143.2.3  paf       319:                if(vheaders=options->get(http_headers_name)) {
1.127     paf       320:                        valid_options++;
                    321:                }
1.143.2.3  paf       322:                if(ValuePtr vany_status=options->get(http_any_status_name)) {
1.142     paf       323:                        valid_options++;
                    324:                        fail_on_status_ne_200=!vany_status->as_bool(); 
                    325:                }
                    326: 
1.143.2.3  paf       327:                if(valid_options!=options->count())
1.127     paf       328:                        throw Exception("parser.runtime",
1.143.2.3  paf       329:                                Exception::undefined_source,
1.127     paf       330:                                "invalid option passed");
1.133     paf       331:        } 
1.126     paf       332: 
                    333:        //making request
1.143.2.3  paf       334:        String request;
                    335:        if(method)
1.143.2.4  paf       336:                request<<method;
1.143.2.3  paf       337:        else
                    338:                request<<"GET";
                    339:        request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; 
1.127     paf       340:        bool user_agent_specified=false;
                    341:        if(vheaders && !vheaders->is_string()) { // allow empty
1.143.2.8  paf       342:                if(HashStringValue *headers=vheaders->get_hash(connect_string)) {
1.143.2.3  paf       343:                        Http_pass_header_info info={&pool, &charset, &request};
1.127     paf       344:                        headers->for_each(http_pass_header, &info); 
                    345:                        user_agent_specified=info.user_agent_specified;
                    346:                } else
                    347:                        throw Exception("parser.runtime", 
1.143.2.3  paf       348:                                connect_string,
1.127     paf       349:                                "headers param must be hash"); 
                    350:        };
                    351:        if(!user_agent_specified) // defaulting
                    352:                request << "user-agent: " DEFAULT_USER_AGENT "\n";
1.126     paf       353:        request<<"\n"; 
                    354:        
                    355:        //sending request
1.143.2.3  paf       356:        String response;
1.143.2.5  paf       357:        CharPtr request_cstr=request.cstr(String::UL_UNSPECIFIED);
1.143.2.3  paf       358:        int status_code=http_request(pool, response,
1.143.2.4  paf       359:                connect_string, host, port, request_cstr, 
1.142     paf       360:                timeout, fail_on_status_ne_200); 
1.126     paf       361:        
                    362:        //processing results    
                    363:        int pos=response.pos("\r\n\r\n", 4); 
                    364:        if(pos<1){
                    365:                throw Exception("http.response", 
1.143.2.3  paf       366:                        connect_string,
1.126     paf       367:                        "bad response from host - no headers found"); 
                    368:        }
1.143.2.3  paf       369:        StringPtr header_block=response.mid(0, pos); 
                    370:        StringPtr body=response.mid(pos+4, response.size()); 
1.126     paf       371:        
1.143.2.9  paf       372:        Array<StringPtr> aheaders;
1.143.2.3  paf       373:        HashStringValuePtr headers(new HashStringValue); 
                    374:        header_block->split(aheaders, 0, "\r\n", 2); 
1.126     paf       375:        
                    376:        //processing headers
1.143.2.3  paf       377:        for(int i=1; i<aheaders.count(); i++) {
1.143.2.9  paf       378:                StringPtr line=aheaders.get(i);
1.143.2.3  paf       379:                pos=line->pos(": ", 2); 
                    380:                if(pos<1)
1.126     paf       381:                        throw Exception("http.response", 
1.143.2.3  paf       382:                                connect_string,
1.143.2.12! paf       383:                                "bad response from host - bad header \"%s\"", line->cstr().get());
1.143.2.3  paf       384:                        
                    385:                headers->put(
                    386:                        line->mid(0, pos)->change_case(pool, charset, String::CC_UPPER), 
                    387:                        ValuePtr(new VString(line->mid(pos+2, line->size())))); 
1.126     paf       388:        }
                    389: 
                    390:        // output response
1.143.2.3  paf       391:        data=new(pool) char[data_size=body->size()];
                    392:        body->store_to(data, String::UL_AS_IS);
                    393:        if(return_outfields) {
1.143.2.4  paf       394:                headers->put(file_status_name, ValuePtr(new VInt(status_code))); 
                    395:                return headers;
                    396:        } else
                    397:                return HashStringValuePtr(0);
1.34      paf       398: }
1.123     paf       399: 
                    400: #ifndef DOXYGEN
                    401: struct File_read_action_info {
1.143.2.4  paf       402:        char **data; size_t *data_size;
1.126     paf       403: }; 
1.123     paf       404: #endif
1.143.2.4  paf       405: static void file_read_action(Pool& pool,
1.126     paf       406:                                                         struct stat& finfo, 
1.123     paf       407:                                                         int f, 
1.143.2.9  paf       408:                                                         StringPtr file_spec, const char* fname, bool as_text, 
1.123     paf       409:                                                         void *context) {
1.126     paf       410:        File_read_action_info& info=*static_cast<File_read_action_info *>(context); 
1.123     paf       411:        if(size_t to_read_size=(size_t)finfo.st_size) { 
1.143.2.4  paf       412:                *info.data=pool.malloc(to_read_size+(as_text?1:0)); 
1.126     paf       413:                *info.data_size=(size_t)read(f, *info.data, to_read_size); 
1.123     paf       414: 
                    415:                if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)
1.126     paf       416:                        throw Exception(0, 
1.143.2.4  paf       417:                                file_spec, 
1.123     paf       418:                                "read failed: actually read %lu bytes count not in [0..%lu] valid range", 
1.126     paf       419:                                        *info.data_size, to_read_size); 
1.123     paf       420:        } else { // empty file
                    421:                if(as_text) {
1.126     paf       422:                        *info.data=pool.malloc(1)1.123     paf       423:                        *(char*)(*info.data)=0;
                    424:                } else 
                    425:                        *info.data=0;
                    426:                *info.data_size=0;
                    427:                return;
                    428:        }
1.126     paf       429: }
1.143.2.9  paf       430: File_read_result file_read(Pool& pool, Charset& charset, StringPtr file_spec, 
1.143.2.4  paf       431:                           char*& data, size_t& data_size, 
1.143.2.3  paf       432:                           bool as_text, HashStringValue *params, HashStringValue** out_fields, 
1.126     paf       433:                           bool fail_on_read_problem) {
1.143.2.4  paf       434:        File_read_result result;
                    435:        if(file_spec->starts_with("http://", 7)) {
1.126     paf       436:                // fail on read problem
1.143.2.4  paf       437:                result.fields=file_read_http(pool, charset, file_spec, data, data_size, params); 
                    438:                result.success=true;
1.126     paf       439:        } else {
                    440:                File_read_action_info info={&data, &data_size}; 
1.143.2.4  paf       441:                result.success=file_read_action_under_lock(pool, file_spec, 
1.126     paf       442:                        "read", file_read_action, &info, 
                    443:                        as_text, fail_on_read_problem); 
                    444:        }
1.123     paf       445: 
1.143.2.4  paf       446:        if(result.success && as_text) {
1.131     paf       447:                // UTF-8 signature: EF BB BF
                    448:                if(data_size>=3) {
                    449:                        char *in=(char *)data;
                    450:                        if((in[0] == '\xEF') && (in[1] == '\xBB') &&
                    451:                                (in[2] == '\xBF')) {
                    452:                                data=in+3; data_size-=3;// skip prefix
                    453:                        }
                    454:                }
                    455: 
1.126     paf       456:                fix_line_breaks((char *)(data), data_size); 
1.123     paf       457:                // note: after fixing
1.126     paf       458:                ((char*&)(data))[data_size]=0;
1.123     paf       459:        }
1.126     paf       460: 
                    461:        return result;
1.123     paf       462: }
                    463: 
1.143.2.9  paf       464: bool file_read_action_under_lock(Pool& pool, StringPtr file_spec, 
1.126     paf       465:                                const char* action_name, File_read_action action, void *context, 
                    466:                                bool as_text, 
1.123     paf       467:                                bool fail_on_read_problem) {
1.143.2.5  paf       468:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.33      paf       469:        int f;
                    470: 
                    471:        // first open, next stat:
1.45      paf       472:        // directory update of NTFS hard links performed on open.
1.33      paf       473:        // ex: 
                    474:        //   a.html:^test[] and b.html hardlink to a.html
                    475:        //   user inserts ! before ^test in a.html
1.126     paf       476:        //   directory entry of b.html in NTFS not updated at once, 
1.35      paf       477:        //   they delay update till open, so we would receive "!^test[" string
                    478:        //   if would do stat, next open.
1.123     paf       479:        // later: it seems, even this does not help sometimes
1.98      paf       480:     if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.123     paf       481:                try {
                    482:                        if(lock_shared_blocking(f)!=0)
1.126     paf       483:                                throw Exception("file.lock", 
1.143.2.4  paf       484:                                                file_spec, 
1.123     paf       485:                                                "shared lock failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       486:                                                        strerror(errno), errno, fname.get());
1.123     paf       487: 
1.124     paf       488:                        struct stat finfo;
                    489:                        if(stat(fname, &finfo)!=0)
                    490:                                throw Exception("file.missing", // hardly possible: we just opened it OK
1.143.2.4  paf       491:                                        file_spec, 
1.124     paf       492:                                        "stat failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       493:                                                strerror(errno), errno, fname.get());
1.124     paf       494: 
1.140     paf       495: #ifdef PA_SAFE_MODE
                    496:                        if(finfo.st_uid/*foreign?*/!=geteuid()
                    497:                                && finfo.st_gid/*foreign?*/!=getegid())
1.126     paf       498:                                throw Exception("parser.runtime", 
1.143.2.4  paf       499:                                        file_spec, 
1.140     paf       500:                                        "parser is in safe mode: reading files of foreign group and user disabled [recompile parser with --disable-safe-mode configure option], actual filename '%s'", 
1.143.2.12! paf       501:                                                fname.get());
1.105     paf       502: #endif
1.32      paf       503: 
1.126     paf       504:                        action(pool, finfo, f, file_spec, fname, as_text, context); 
1.123     paf       505:                } catch(...) {
1.126     paf       506:                        unlock(f);close(f); 
1.123     paf       507:                        if(fail_on_read_problem)
1.143.2.11  paf       508:                                rethrow;
1.123     paf       509:                        return false;                   
                    510:                } 
1.87      paf       511: 
1.126     paf       512:                unlock(f);close(f); 
1.72      parser    513:                return true;
1.118     paf       514:     } else {
                    515:                if(fail_on_read_problem)
1.126     paf       516:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       517:                                file_spec, 
1.123     paf       518:                                "%s failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       519:                                        action_name, strerror(errno), errno, fname.get());
1.118     paf       520:                return false;
                    521:        }
1.8       paf       522: }
                    523: 
1.143.2.9  paf       524: static void create_dir_for_file(StringPtr file_spec) {
1.63      parser    525:        size_t pos_after=1;
                    526:        int pos_before;
1.143.2.4  paf       527:        while((pos_before=file_spec->pos("/", 1, pos_after))>=0) {
                    528:                mkdir(file_spec->mid(0, pos_before)->cstr(String::UL_FILE_SPEC), 0775); 
1.63      parser    529:                pos_after=pos_before+1;
                    530:        }
                    531: }
                    532: 
1.98      paf       533: bool file_write_action_under_lock(
1.143.2.9  paf       534:                                StringPtr file_spec, 
1.126     paf       535:                                const char* action_name, File_write_action action, void *context, 
                    536:                                bool as_text, 
                    537:                                bool do_append, 
                    538:                                bool do_block, 
1.110     paf       539:                                bool fail_on_lock_problem) {
1.143.2.5  paf       540:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.28      paf       541:        int f;
1.80      paf       542:        if(access(fname, W_OK)!=0) // no
1.126     paf       543:                create_dir_for_file(file_spec); 
1.50      paf       544: 
1.80      paf       545:        if((f=open(fname, 
                    546:                O_CREAT|O_RDWR
                    547:                |(as_text?_O_TEXT:_O_BINARY)
1.138     paf       548:                |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) {
1.99      paf       549:                if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.126     paf       550:                        Exception e("file.lock", 
1.143.2.4  paf       551:                                file_spec, 
1.110     paf       552:                                "shared lock failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       553:                                strerror(errno), errno, fname.get());
1.126     paf       554:                        close(f); 
1.110     paf       555:                        if(fail_on_lock_problem)
                    556:                                throw e;
1.98      paf       557:                        return false;
                    558:                }
1.96      paf       559: 
                    560:                try {
1.126     paf       561:                        action(f, context); 
1.96      paf       562:                } catch(...) {
1.138     paf       563: #ifdef HAVE_FTRUNCATE
1.104     paf       564:                        if(!do_append)
1.125     paf       565:                                ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower
1.138     paf       566: #endif
1.126     paf       567:                        unlock(f);close(f); 
1.143.2.11  paf       568:                        rethrow;
1.96      paf       569:                }
1.80      paf       570:                
1.138     paf       571: #ifdef HAVE_FTRUNCATE
1.104     paf       572:                if(!do_append)
1.125     paf       573:                        ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.138     paf       574: #endif
1.126     paf       575:                unlock(f);close(f); 
1.98      paf       576:                return true;
1.80      paf       577:        } else
1.126     paf       578:                throw Exception(errno==EACCES?"file.access":0, 
1.143.2.4  paf       579:                        file_spec, 
1.96      paf       580:                        "%s failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       581:                                action_name, strerror(errno), errno, fname.get());
1.96      paf       582:        // here should be nothing, see rethrow above
                    583: }
                    584: 
                    585: #ifndef DOXYGEN
                    586: struct File_write_action_info {
                    587:        const void *data; size_t size;
1.126     paf       588: }; 
1.96      paf       589: #endif
                    590: static void file_write_action(int f, void *context) {
1.126     paf       591:        File_write_action_info& info=*static_cast<File_write_action_info *>(context); 
1.113     paf       592:        if(info.size) {
1.126     paf       593:                int written=write(f, info.data, info.size); 
1.116     paf       594:                if(written<0)
1.126     paf       595:                        throw Exception(0, 
1.143.2.4  paf       596:                                Exception::undefined_source, 
1.126     paf       597:                                "write failed: %s (%d)",  strerror(errno), errno); 
1.113     paf       598:        }
1.96      paf       599: }
                    600: void file_write(
1.143.2.9  paf       601:                                StringPtr file_spec, 
1.96      paf       602:                                const void *data, size_t size, 
1.126     paf       603:                                bool as_text, 
1.96      paf       604:                                bool do_append) {
1.126     paf       605:        File_write_action_info info={data, size}; 
1.98      paf       606:        file_write_action_under_lock(
1.143.2.4  paf       607:                file_spec, 
                    608:                "write", file_write_action, &info, 
                    609:                as_text, 
                    610:                do_append); 
1.30      paf       611: }
                    612: 
1.63      parser    613: // throws nothing! [this is required in file_move & file_delete]
1.143.2.9  paf       614: static void rmdir(StringPtr file_spec, size_t pos_after) {
1.50      paf       615:        int pos_before;
1.143.2.4  paf       616:        if((pos_before=file_spec->pos("/", 1, pos_after))>=0)
1.126     paf       617:                rmdir(file_spec, pos_before+1); 
1.50      paf       618:        
1.143.2.4  paf       619:        rmdir(file_spec->mid(0, pos_after-1/* / */)->cstr(String::UL_FILE_SPEC)); 
1.50      paf       620: }
1.143.2.9  paf       621: bool file_delete(StringPtr file_spec, bool fail_on_read_problem) {
1.143.2.5  paf       622:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.54      parser    623:        if(unlink(fname)!=0)
1.93      paf       624:                if(fail_on_read_problem)
1.126     paf       625:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       626:                                file_spec, 
1.93      paf       627:                                "unlink failed: %s (%d), actual filename '%s'", 
1.143.2.12! paf       628:                                        strerror(errno), errno, fname.get());
1.93      paf       629:                else
                    630:                        return false;
1.50      paf       631: 
1.126     paf       632:        rmdir(file_spec, 1); 
1.93      paf       633:        return true;
1.60      parser    634: }
1.143.2.9  paf       635: void file_move(StringPtr old_spec, StringPtr new_spec) {
1.143.2.5  paf       636:        CharPtr old_spec_cstr=old_spec->cstr(String::UL_FILE_SPEC); 
                    637:        CharPtr new_spec_cstr=new_spec->cstr(String::UL_FILE_SPEC); 
1.63      parser    638:        
1.126     paf       639:        create_dir_for_file(new_spec); 
1.63      parser    640: 
1.60      parser    641:        if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126     paf       642:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       643:                        old_spec, 
1.60      parser    644:                        "rename failed: %s (%d), actual filename '%s' to '%s'", 
1.143.2.12! paf       645:                                strerror(errno), errno, old_spec_cstr.get(), new_spec_cstr.get());
1.63      parser    646: 
1.126     paf       647:        rmdir(old_spec, 1); 
1.31      paf       648: }
                    649: 
1.51      paf       650: 
1.126     paf       651: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118     paf       652:        struct stat lfinfo;
                    653:        bool result=stat(fname, &lfinfo)==0;
                    654:        if(afinfo)
                    655:                *afinfo=lfinfo;
                    656:        return result;
1.119     paf       657: }
                    658: 
1.143.2.9  paf       659: bool entry_exists(StringPtr file_spec) {
1.143.2.5  paf       660:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.126     paf       661:        return entry_exists(fname, 0); 
1.118     paf       662: }
                    663: 
1.143.2.9  paf       664: static bool entry_readable(StringPtr file_spec, bool need_dir) {
1.143.2.4  paf       665:     CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.120     paf       666:        if(need_dir) {
1.126     paf       667:                size_t size=strlen(fname); 
1.120     paf       668:                while(size) {
1.126     paf       669:                        char c=fname[size-1]; 
1.120     paf       670:                        if(c=='/' || c=='\\')
                    671:                                fname[--size]=0;
                    672:                        else
                    673:                                break;
                    674:                }
                    675:        }
1.51      paf       676:        struct stat finfo;
1.118     paf       677:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109     paf       678:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       679:                return is_dir==need_dir;
                    680:        }
                    681:        return false;
                    682: }
1.143.2.9  paf       683: bool file_readable(StringPtr file_spec) {
1.126     paf       684:        return entry_readable(file_spec, false); 
1.51      paf       685: }
1.143.2.9  paf       686: bool dir_readable(StringPtr file_spec) {
1.126     paf       687:        return entry_readable(file_spec, true); 
1.65      parser    688: }
1.143.2.9  paf       689: StringPtr file_readable(StringPtr path, const String& name) {
1.143.2.4  paf       690:        StringPtr result(new String(*path));
1.126     paf       691:        *result << "/"; 
1.65      parser    692:        *result << name;
1.143.2.9  paf       693:        return file_readable(result)?result:StringPtr(0);
1.43      paf       694: }
1.143.2.9  paf       695: bool file_executable(StringPtr file_spec) {
1.143.2.4  paf       696:     return access(file_spec->cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44      paf       697: }
                    698: 
1.143.2.9  paf       699: bool file_stat(StringPtr file_spec, 
1.58      parser    700:                           size_t& rsize, 
1.126     paf       701:                           time_t& ratime, 
                    702:                           time_t& rmtime, 
                    703:                           time_t& rctime, 
1.64      parser    704:                           bool fail_on_read_problem) {
1.143.2.5  paf       705:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.44      paf       706:     struct stat finfo;
                    707:        if(stat(fname, &finfo)!=0)
1.64      parser    708:                if(fail_on_read_problem)
1.126     paf       709:                        throw Exception("file.missing", 
1.143.2.4  paf       710:                                file_spec, 
1.67      parser    711:                                "getting file size failed: %s (%d), real filename '%s'", 
1.143.2.12! paf       712:                                        strerror(errno), errno, fname.get());
1.64      parser    713:                else
                    714:                        return false;
1.58      parser    715:        rsize=finfo.st_size;
                    716:        ratime=finfo.st_atime;
                    717:        rmtime=finfo.st_mtime;
                    718:        rctime=finfo.st_ctime;
1.64      parser    719:        return true;
1.18      paf       720: }
                    721: 
1.126     paf       722: char* getrow(char* *row_ref, char delim) {
                    723:     char* result=*row_ref;
1.8       paf       724:     if(result) {
1.126     paf       725:                *row_ref=strchr(result, delim); 
1.8       paf       726:                if(*row_ref) 
                    727:                        *((*row_ref)++)=0; 
                    728:                else if(!*result) 
                    729:                        return 0;
                    730:     }
                    731:     return result;
                    732: }
                    733: 
1.126     paf       734: char* lsplit(char* string, char delim) {
1.23      paf       735:     if(string) {
1.126     paf       736:                char* v=strchr(string, delim); 
1.8       paf       737:                if(v) {
                    738:                        *v=0;
                    739:                        return v+1;
                    740:                }
                    741:     }
                    742:     return 0;
                    743: }
                    744: 
1.126     paf       745: char* lsplit(char* *string_ref, char delim) {
                    746:     char* result=*string_ref;
                    747:        char* next=lsplit(*string_ref, delim); 
1.8       paf       748:     *string_ref=next;
                    749:     return result;
1.9       paf       750: }
                    751: 
1.126     paf       752: char* rsplit(char* string, char delim) {
1.18      paf       753:     if(string) {
1.126     paf       754:                char* v=strrchr(string, delim); 
1.18      paf       755:                if(v) {
1.9       paf       756:                        *v=0;
                    757:                        return v+1;
                    758:                }
                    759:     }
                    760:     return NULL;       
1.10      paf       761: }
                    762: 
1.37      paf       763: /// @todo less stupid type detection
1.143.2.10  paf       764: const char* format(Pool& pool, double value, char* fmt) {
1.126     paf       765:        char local_buf[MAX_NUMBER]; 
1.108     paf       766:        size_t size;
                    767:        
1.10      paf       768:        if(fmt)
                    769:                if(strpbrk(fmt, "diouxX"))
                    770:                        if(strpbrk(fmt, "ouxX"))
1.126     paf       771:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); 
1.10      paf       772:                        else
1.126     paf       773:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); 
1.10      paf       774:                else
1.126     paf       775:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value); 
1.10      paf       776:        else
1.126     paf       777:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); 
1.10      paf       778:        
1.143.2.7  paf       779:        return pool.copy(local_buf, size+1); 
1.12      paf       780: }
                    781: 
1.36      paf       782: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       783: #ifdef WIN32
                    784:        do{
1.126     paf       785:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout); 
1.12      paf       786:                if(chunk_written<=0)
                    787:                        break;
                    788:                size-=chunk_written;
1.36      paf       789:                buf=((const char*)buf)+chunk_written;
1.126     paf       790:        } while(size>0); 
1.12      paf       791: 
                    792:        return size;
                    793: #else
1.126     paf       794:        return fwrite(buf, 1, size, stdout); 
1.12      paf       795: #endif
1.2       paf       796: }
1.14      paf       797: 
1.143.2.4  paf       798: char* unescape_chars(Pool& pool, const char* cp, int len) {
                    799:        char* s=pool.malloc(len + 1); 
1.14      paf       800:        enum EscapeState {
1.33      paf       801:                EscapeRest, 
                    802:                EscapeFirst, 
1.14      paf       803:                EscapeSecond
                    804:        } escapeState=EscapeRest;
                    805:        int escapedValue=0;
                    806:        int srcPos=0;
                    807:        int dstPos=0;
                    808:        while(srcPos < len) {
1.126     paf       809:                int ch=cp[srcPos]; 
1.14      paf       810:                switch(escapeState) {
                    811:                        case EscapeRest:
                    812:                        if(ch=='%') {
                    813:                                escapeState=EscapeFirst;
                    814:                        } else if(ch=='+') {
1.126     paf       815:                                s[dstPos++]=' '; 
1.14      paf       816:                        } else {
                    817:                                s[dstPos++]=ch; 
                    818:                        }
                    819:                        break;
                    820:                        case EscapeFirst:
                    821:                        escapedValue=hex_value[ch] << 4;        
                    822:                        escapeState=EscapeSecond;
                    823:                        break;
                    824:                        case EscapeSecond:
1.126     paf       825:                        escapedValue +=hex_value[ch]; 
1.14      paf       826:                        s[dstPos++]=escapedValue;
                    827:                        escapeState=EscapeRest;
                    828:                        break;
                    829:                }
1.126     paf       830:                srcPos++; 
1.14      paf       831:        }
                    832:        s[dstPos]=0;
                    833:        return s;
1.24      paf       834: }
                    835: 
                    836: #ifdef WIN32
1.126     paf       837: void back_slashes_to_slashes(char* s) {
1.24      paf       838:        if(s)
                    839:                for(; *s; s++)
                    840:                        if(*s=='\\')
1.126     paf       841:                                *s='/'; 
1.24      paf       842: }
1.42      paf       843: /*
1.126     paf       844: void slashes_to_back_slashes(char* s) {
1.42      paf       845:        if(s)
                    846:                for(; *s; s++)
                    847:                        if(*s=='/')
1.126     paf       848:                                *s='\\'; 
1.42      paf       849: }
                    850: */
1.24      paf       851: #endif
1.41      paf       852: 
1.126     paf       853: bool StrEqNc(const char* s1, const char* s2, bool strict) {
1.41      paf       854:        while(true) {
                    855:                if(!(*s1)) {
                    856:                        if(!(*s2))
                    857:                                return true;
                    858:                        else
                    859:                                return !strict;
                    860:                } else if(!(*s2))
                    861:                        return !strict;
                    862:                if(isalpha(*s1)) {
                    863:                        if(tolower(*s1) !=tolower(*s2))
                    864:                                return false;
                    865:                } else if((*s1) !=(*s2))
                    866:                        return false;
1.126     paf       867:                s1++; 
                    868:                s2++; 
1.41      paf       869:        }
1.57      parser    870: }
                    871: 
1.84      paf       872: static bool isLeap(int year) {
1.57      parser    873:     return !(
                    874:              (year % 4) || ((year % 400) && !(year % 100))
1.126     paf       875:             ); 
1.57      parser    876: }
                    877: 
                    878: int getMonthDays(int year, int month) {
                    879:     int monthDays[]={
1.126     paf       880:         31, 
                    881:         isLeap(year) ? 29 : 28, 
                    882:         31, 
                    883:         30, 
                    884:         31, 
                    885:         30, 
                    886:         31, 
                    887:         31, 
                    888:         30, 
                    889:         31, 
                    890:         30, 
1.57      parser    891:         31
1.126     paf       892:     }; 
                    893:     return monthDays[month]; 
1.41      paf       894: }
1.69      parser    895: 
1.126     paf       896: void remove_crlf(char* start, char* end) {
                    897:        for(char* p=start; p<end; p++)
1.69      parser    898:                switch(*p) {
1.126     paf       899:                        case '\n': *p='|';  break;
                    900:                        case '\r': *p=' ';  break;
1.69      parser    901:                }
1.91      paf       902: }
                    903: 
                    904: 
                    905: /// must be last in this file
                    906: #undef vsnprintf
1.126     paf       907: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91      paf       908:        if(!s)
                    909:                return 0;
                    910: 
                    911:        int r;
                    912:        // note: on win32& maybe somewhere else
                    913:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    914:        --s;
                    915: #if _MSC_VER
                    916:        /*
                    917:        win32: 
                    918:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    919: 
1.143.2.12! paf       920:          if the number of bytes to write exceeds buffer, then count bytes are written and Ö1 is returned
1.91      paf       921:        */
1.126     paf       922:        r=_vsnprintf(b, s, f, l); 
1.91      paf       923:        if(r<0) 
                    924:                r=s;
                    925: #else
1.126     paf       926:        r=vsnprintf(b, s, f, l); 
1.91      paf       927:        /*
                    928:        solaris: 
                    929:        man vsnprintf
                    930: 
                    931:          The snprintf() function returns  the  number  of  characters
                    932:        formatted, that is, the number of characters that would have
                    933:        been written to the buffer if it were large enough.  If  the
                    934:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    935:        value less than 1 is returned.
                    936:        */
                    937: 
                    938:        if(r<0)
                    939:                r=0;
                    940:        else if(r>s)
                    941:                r=s;
                    942: #endif
                    943:        b[r]=0;
                    944:        return r;
                    945: }
                    946: 
1.126     paf       947: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91      paf       948:        va_list l;
1.126     paf       949:     va_start(l, f); 
                    950:     int r=__vsnprintf(b, s, f, l); 
                    951:     va_end(l); 
1.91      paf       952:        return r;
1.98      paf       953: }
                    954: 
                    955: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.126     paf       956:        for (;  usecs >= 1000000; ++secs, usecs -= 1000000); 
1.98      paf       957: 
                    958: #ifdef WIN32
1.126     paf       959:        Sleep(secs * 1000 + usecs / 1000); 
1.98      paf       960:        return 0;
                    961: #else
                    962:        struct timeval t;
                    963:        t.tv_sec = secs;
                    964:        t.tv_usec = usecs;
1.126     paf       965:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0); 
1.98      paf       966: #endif
1.135     paf       967: }
                    968: 
                    969: 

E-mail: