--- parser3/src/main/pa_common.C 2003/01/23 17:05:34 1.143.2.1 +++ parser3/src/main/pa_common.C 2003/03/21 07:07:06 1.146 @@ -5,12 +5,14 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2003/01/23 17:05:34 $"; +static const char* IDENT_COMMON_C="$Date: 2003/03/21 07:07:06 $"; #include "pa_common.h" #include "pa_exception.h" #include "pa_globals.h" #include "pa_hash.h" +#include "pa_vstring.h" +#include "pa_vdate.h" #ifdef WIN32 # include @@ -113,13 +115,11 @@ void fix_line_breaks(char* buf, size_t& memcpy(dest, bol, eob-bol); } -char* file_read_text(Pool& pool, - ConstStringPtr file_spec, +char* file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem, - StringHash *params, StringHash** out_fields) { + Hash *params, Hash** out_fields) { void *result; size_t size; - return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)? - (char *)result:0; + return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)?(char *)result:0; } //http request stuff @@ -139,11 +139,11 @@ static bool set_addr(struct sockaddr_in return true; } -static int http_read_response(Pool& pool, String& response, int sock, bool fail_on_status_ne_200){ +static int http_read_response(String& response, int sock, bool fail_on_status_ne_200){ const String* status_code=0; ssize_t EOLat=0; while(true) { - char *buf=(char *)pool.malloc(MAX_STRING); + char *buf=(char *)response.pool().malloc(MAX_STRING); ssize_t size=recv(sock, buf, MAX_STRING, 0); if(size<=0) break; @@ -171,10 +171,10 @@ static int http_read_response(Pool& pool /* ********************** request *************************** */ #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP) -# define WE_CAN_USE_ALARM +# define PA_USE_ALARM #endif -#ifdef WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM static sigjmp_buf timeout_env; static void timeout_handler(int sig){ siglongjmp(timeout_env, 1); @@ -192,23 +192,29 @@ static int http_request(String& response origin_string, "zero hostname"); //never -#ifdef WE_CAN_USE_ALARM - signal(SIGALRM, timeout_handler); +#ifdef PA_USE_ALARM + signal(SIGALRM, timeout_handler); #endif int sock=-1; - try { - int result; -#ifdef WE_CAN_USE_ALARM - if(sigsetjmp(timeout_env, 1)) - throw Exception("http.timeout", - origin_string, - "timeout occured while retrieving document"); - else { - alarm(timeout); +#ifdef PA_USE_ALARM + if(sigsetjmp(timeout_env, 1)) { + // stupid gcc [2.95.4] generated bad code + // which failed to handle sigsetjmp+throw: crashed inside of pre-throw code. + // rewritten simplier [though duplicating closesocket code] + if(sock>=0) + closesocket(sock); + throw Exception("http.timeout", + origin_string, + "timeout occured while retrieving document"); + return 0; // never + } else { + alarm(timeout); #endif + try { + int result; struct sockaddr_in dest; - - if(!set_addr(&dest, host, port)) + + if(!set_addr(&dest, host, port)) throw Exception("http.host", origin_string, "can not resolve hostname \"%s\"", host); @@ -229,18 +235,18 @@ static int http_request(String& response result=http_read_response(response, sock, fail_on_status_ne_200); closesocket(sock); -#ifdef WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM alarm(0); - } #endif - return result; - } catch(...) { - if(sock>=0) - closesocket(sock); -#ifdef WE_CAN_USE_ALARM - alarm(0); + } catch(...) { +#ifdef PA_USE_ALARM + alarm(0); #endif - /*re*/throw; + if(sock>=0) + closesocket(sock); + /*re*/throw; + } + return result; } } @@ -273,7 +279,7 @@ static void file_read_http(Pool& pool, c bool fail_on_status_ne_200=true; Value *vheaders=0; - String connect_string(pool); + String& connect_string=*new(pool) String(pool); // must not be local [exception may be reported outside] // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces @@ -385,7 +391,7 @@ struct File_read_action_info { void **data; size_t *data_size; }; #endif -static void file_read_action( +static void file_read_action(Pool& pool, struct stat& finfo, int f, const String& file_spec, const char* fname, bool as_text, @@ -410,7 +416,7 @@ static void file_read_action( return; } } -bool file_read(ConstStringPtr file_spec, +bool file_read(Pool& pool, const String& file_spec, void*& data, size_t& data_size, bool as_text, Hash *params, Hash** out_fields, bool fail_on_read_problem) { @@ -444,7 +450,7 @@ bool file_read(ConstStringPtr file_spec, return result; } -bool file_read_action_under_lock(const String& file_spec, +bool file_read_action_under_lock(Pool& pool, const String& file_spec, const char* action_name, File_read_action action, void *context, bool as_text, bool fail_on_read_problem) { @@ -745,7 +751,7 @@ char* rsplit(char* string, char delim) { } /// @todo less stupid type detection -char* format(double value, char* fmt) { +char* format(Pool& pool, double value, char* fmt) { char local_buf[MAX_NUMBER]; size_t size; @@ -781,7 +787,7 @@ size_t stdout_write(const void *buf, siz #endif } -char* unescape_chars(const char* cp, int len) { +char* unescape_chars(Pool& pool, const char* cp, int len) { char* s=(char *)pool.malloc(len + 1, 5); enum EscapeState { EscapeRest, @@ -953,3 +959,74 @@ int pa_sleep(unsigned long secs, unsigne } +// attributed meaning + +static size_t date_attribute(const VDate& vdate, char *buf, size_t buf_size) { + const char month_names[12][4]={ + "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; + const char days[7][4]={ + "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; + + time_t when=vdate.get_time(); + struct tm *tms=gmtime(&when); + if(!tms) + throw Exception(0, + 0, + "bad time in attribute value (seconds from epoch=%ld)", when); + return snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT", + days[tms->tm_wday], + tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900, + tms->tm_hour,tms->tm_min,tms->tm_sec); +} +static void append_attribute_meaning(String& result, + Value& value, String::Untaint_lang lang, bool forced) { + if(const String *string=value.get_string()) + result.append(string->join_chains(result.pool(), 0), lang, forced); + else + if(Value *vdate=value.as(VDATE_TYPE, false)) { + char *buf=(char *)result.malloc(MAX_STRING); + size_t size=date_attribute(*static_cast(vdate), + buf, MAX_STRING); + + result.APPEND_CLEAN(buf, size, "converted from date", 0); + } else + throw Exception("parser.runtime", + &result, + "trying to append here neither string nor date (%s)", + value.type()); +} +#ifndef DOXYGEN +struct Attributed_meaning_info { + String *header; // header line being constructed + String::Untaint_lang lang; // language in which to append to that line + bool forced; // do they force that lang? +}; +#endif +static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, + void *info) { + if(akey==VALUE_NAME) + return; + + Attributed_meaning_info& ami=*static_cast(info); + + // ...; charset=windows1251 + *ami.header << "; "; + ami.header->append(akey, ami.lang, ami.forced); + *ami.header << "="; + append_attribute_meaning(*ami.header, *static_cast(avalue), ami.lang, ami.forced); +} +const String& attributed_meaning_to_string(Value& meaning, + String::Untaint_lang lang, bool forced) { + String &result=*new(meaning.pool()) String(meaning.pool()); + if(Hash *hash=meaning.get_hash(0)) { + // $value(value) $subattribute(subattribute value) + if(Value *value=static_cast(hash->get(*value_name))) + append_attribute_meaning(result, *value, lang, forced); + + Attributed_meaning_info attributed_meaning_info={&result, lang}; + hash->for_each(append_attribute_subattribute, &attributed_meaning_info); + } else // result value + append_attribute_meaning(result, meaning, lang, forced); + + return result; +}