1 #ifndef ASIM_AFTERBASE_H_HEADER_INCLUDED 2 #define ASIM_AFTERBASE_H_HEADER_INCLUDED 3 4 #ifdef HAVE_MALLOC_H 5 # include <malloc.h> 6 #endif 7 #ifdef HAVE_STDLIB_H 8 # include <stdlib.h> 9 #endif 10 #if TIME_WITH_SYS_TIME 11 # include <sys/time.h> 12 # include <time.h> 13 #else 14 # if HAVE_SYS_TIME_H 15 # include <sys/time.h> 16 # else 17 # include <time.h> 18 # endif 19 #endif 20 21 22 /* our own version of X Wrapper : */ 23 #include "xwrap.h" 24 25 /* the goal of this header is to provide sufficient code so that 26 libAfterImage could live without libAfterBase at all. 27 Basically with define macros and copy over few functions 28 from libAfterBase 29 */ 30 31 /* from libAfterBase/astypes.h : */ 32 33 #include <stdio.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 40 #ifdef __GNUC__ 41 42 #ifndef MIN 43 #define MIN(x,y) \ 44 ({ const typeof(x) _x = (x); const typeof(y) _y = (y); \ 45 (void) (&_x == &_y); \ 46 _x < _y ? _x : _y; }) 47 #endif 48 #ifndef MAX 49 #define MAX(x,y) \ 50 ({ const typeof(x) _x = (x); const typeof(y) _y = (y); \ 51 (void) (&_x == &_y); \ 52 _x > _y ? _x : _y; }) 53 #endif 54 55 #define AS_ASSERT(p) ((p)==(typeof(p))0) 56 #define AS_ASSERT_NOTVAL(p,v) ((p)!=(typeof(p))v) 57 58 #else 59 60 #define MIN(a,b) ((a)<(b) ? (a) : (b)) 61 #define MAX(a,b) ((a)>(b) ? (a) : (b)) 62 #define AS_ASSERT(p) ((p)==0) 63 #define AS_ASSERT_NOTVAL(p,v) ((p)!=(v)) 64 #define inline 65 66 #endif 67 68 #ifdef __INTEL_COMPILER 69 #define inline 70 #endif 71 72 #ifndef max 73 #define max(x,y) MAX(x,y) 74 #endif 75 76 #ifndef min 77 #define min(x,y) MIN(x,y) 78 #endif 79 80 typedef unsigned long ASFlagType ; 81 #define ASFLAGS_EVERYTHING 0xFFFFFFFF 82 typedef ASFlagType ASFlagsXref[5]; 83 84 #define get_flags(var, val) ((var) & (val)) /* making it sign safe */ 85 #define set_flags(var, val) ((var) |= (val)) 86 #define clear_flags(var, val) ((var) &= ~(val)) 87 #define CheckSetFlag(b,f,v) {if((b)) (f) |= (v) ; else (f) &= ~(v);} 88 89 #define PTR2CARD32(p) ((CARD32)(p)) 90 #define LONG2CARD32(l) ((CARD32)(l)) 91 92 typedef struct ASMagic 93 { /* just so we can safely cast void* to query magic number :*/ 94 unsigned long magic ; 95 }ASMagic; 96 97 /* from libAfterBase/selfdiag.h : */ 98 #define get_caller_func() "unknown" 99 100 /* from libAfterBase/output.h : */ 101 /* user app must export these if libAfterBase is not used : */ 102 void asim_set_application_name (char *argv0); 103 const char *asim_get_application_name(); 104 105 #define set_application_name asim_set_application_name 106 #define get_application_name asim_get_application_name 107 108 /* 109 * FEW PRESET LEVELS OF OUTPUT : 110 */ 111 #define OUTPUT_LEVEL_INVALID 0 112 #define OUTPUT_LEVEL_PARSE_ERR 1 113 #define OUTPUT_LEVEL_ERROR 1 114 #define OUTPUT_LEVEL_WARNING 4 115 #define OUTPUT_DEFAULT_THRESHOLD 5 116 #define OUTPUT_LEVEL_PROGRESS OUTPUT_DEFAULT_THRESHOLD 117 #define OUTPUT_LEVEL_ACTIVITY OUTPUT_DEFAULT_THRESHOLD 118 #define OUTPUT_VERBOSE_THRESHOLD 6 119 #define OUTPUT_LEVEL_DEBUG 10 /* anything above it is hardcore debugging */ 120 121 /* AfterStep specific error and Warning handlers : */ 122 /* Returns True if something was actually printed */ 123 unsigned int asim_get_output_threshold(); 124 unsigned int asim_set_output_threshold( unsigned int threshold ); 125 #define get_output_threshold asim_get_output_threshold 126 #define set_output_threshold asim_set_output_threshold 127 128 Bool asim_show_error( const char *error_format, ...); 129 Bool asim_show_warning( const char *warning_format, ...); 130 Bool asim_show_progress( const char *msg_format, ...); 131 Bool asim_show_debug( const char *file, const char *func, int line, const char *msg_format, ...); 132 133 #define show_error asim_show_error 134 #define show_warning asim_show_warning 135 #define show_progress asim_show_progress 136 #define show_debug asim_show_debug 137 138 void asim_nonGNUC_debugout( const char *format, ...); 139 void asim_nonGNUC_debugout_stub( const char *format, ...); 140 /* may be used below in case compilation problems occur. 141 * Please submit a bug report if usage of any of the following generates errors on 142 * your compiler . Thanks!!! */ 143 144 /* Some usefull debugging macros : */ 145 #ifdef __GNUC__ 146 147 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL)) 148 #define DEBUG_OUT(format,args...) \ 149 do{ fprintf( stderr, "%s:%s:%s:%d:>" format "\n", get_application_name(), __FILE__, __FUNCTION__, __LINE__, ## args );}while(0) 150 #else 151 #define DEBUG_OUT(format,args...) 152 #endif /* DEBUG */ 153 154 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL)) 155 #define LOCAL_DEBUG_OUT(format,args...) \ 156 do{ fprintf( stderr, "%s:%s:%s:%d:>" format "\n", get_application_name(), __FILE__, __FUNCTION__, __LINE__, ## args );}while(0) 157 #define LOCAL_DEBUG_CALLER_OUT(format,args...) \ 158 do{ fprintf( stderr, "%s:%s:%s:> called from [%s] with args(" format ")\n", get_application_name(), __FILE__, __FUNCTION__, get_caller_func(), ## args );}while(0) 159 #else 160 #define LOCAL_DEBUG_OUT(format,args...) 161 #define LOCAL_DEBUG_CALLER_OUT(format,args...) 162 #endif /* LOCAL_DEBUG */ 163 164 #elif __STDC_VERSION__ >= 199901 /* C99 standard provides support for this as well : */ 165 166 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL)) 167 #define DEBUG_OUT(...) \ 168 do{ fprintf( stderr, "%s:%s:%s:%d:>", get_application_name(), __FILE__, __FUNCTION__, __LINE__ ); \ 169 fprintf( stderr, __VA_ARGS__); \ 170 fprintf( stderr, "\n"); \ 171 }while(0) 172 #else 173 #define DEBUG_OUT(...) 174 #endif /* DEBUG */ 175 176 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL)) 177 #define LOCAL_DEBUG_OUT(...) \ 178 do{ fprintf( stderr, "%s:%s:%s:%d:>", get_application_name(), __FILE__, __FUNCTION__, __LINE__ ); \ 179 fprintf( stderr, __VA_ARGS__); \ 180 fprintf( stderr, "\n"); \ 181 }while(0) 182 #define LOCAL_DEBUG_CALLER_OUT(...) \ 183 do{ fprintf( stderr, "%s:%s:%s:> called from [%s] with args(", get_application_name(), __FILE__, get_caller_func() ); \ 184 fprintf( stderr, __VA_ARGS__); \ 185 fprintf( stderr, ")\n"); \ 186 }while(0) 187 #else 188 #define LOCAL_DEBUG_OUT(...) 189 #define LOCAL_DEBUG_CALLER_OUT(...) 190 #endif /* LOCAL_DEBUG */ 191 192 #else /* non __GNUC__ or C99 compliant compiler : */ 193 194 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL)) 195 #define DEBUG_OUT asim_nonGNUC_debugout 196 #else 197 #define DEBUG_OUT asim_nonGNUC_debugout_stub 198 #endif /* DEBUG */ 199 200 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL)) 201 #define LOCAL_DEBUG_OUT asim_nonGNUC_debugout 202 #define LOCAL_DEBUG_CALLER_OUT asim_nonGNUC_debugout_stub 203 #else 204 #define LOCAL_DEBUG_OUT asim_nonGNUC_debugout_stub 205 #define LOCAL_DEBUG_CALLER_OUT asim_nonGNUC_debugout_stub 206 #endif /* LOCAL_DEBUG */ 207 208 #endif 209 210 #if defined(DO_CLOCKING) && !defined(NO_DEBUG_OUTPUT) 211 #define START_TIME(started) time_t started = clock() 212 #define SHOW_TIME(s,started) fprintf (stderr, "%s " s " time (clocks): %lu mlsec\n", __FUNCTION__, ((clock () - (started))*100)/CLOCKS_PER_SEC) 213 #else 214 #define START_TIME(started) unsigned long started = 0 215 #define SHOW_TIME(s,started) started = 0 216 #endif 217 218 /* from libAfterBase/safemalloc.h : */ 219 #define safemalloc(s) malloc(s) 220 #define safecalloc(c,s) calloc(c,s) 221 #define safefree(m) free(m) 222 #define NEW(a) ((a *)malloc(sizeof(a))) 223 #define NEW_ARRAY_ZEROED(a,b) ((a *)calloc((b), sizeof(a))) 224 #define NEW_ARRAY(a,b) ((a *)malloc((b)*sizeof(a))) 225 226 /* from libAfterBase/mystring.h : */ 227 228 char *asim_mystrdup (const char *str); 229 char *asim_mystrndup(const char *str, size_t n); 230 int asim_mystrcasecmp (const char *s1, const char *s2); 231 int asim_mystrncasecmp (const char *s1, const char *s2, size_t n); 232 #define mystrdup(s) asim_mystrdup(s) 233 #define mystrndup(s,n) asim_mystrndup(s,n) 234 #define mystrncasecmp(s,s2,n) asim_mystrncasecmp(s,s2,n) 235 #define mystrcasecmp(s,s2) asim_mystrcasecmp(s,s2) 236 237 /* from libAfterBase/fs.h : */ 238 #if !defined(S_IFREG) || !defined(S_IFDIR) 239 # include <sys/stat.h> 240 #endif 241 242 #ifndef _WIN32 243 struct direntry 244 { 245 mode_t d_mode; /* S_IFDIR if a directory */ 246 time_t d_mtime; 247 char d_name[1]; 248 }; 249 #endif 250 int asim_check_file_mode (const char *file, int mode); 251 #define CheckFile(f) asim_check_file_mode(f,S_IFREG) 252 #define CheckDir(d) asim_check_file_mode(d,S_IFDIR) 253 char *asim_put_file_home (const char *path_with_home); 254 #define put_file_home(p) asim_put_file_home(p) 255 char *asim_load_file (const char *realfilename); 256 #define load_file(r) asim_load_file(r) 257 char *asim_load_binary_file(const char* realfilename, long *file_size_return); 258 #define load_binary_file(r,s) asim_load_binary_file(r,s) 259 #ifndef _WIN32 260 int asim_my_scandir_ext ( const char *dirname, int (*filter_func) (const char *), 261 Bool (*handle_direntry_func)( const char *fname, const char *fullname, struct stat *stat_info, void *aux_data), 262 void *aux_data); 263 #define my_scandir_ext(d,f,h,a) asim_my_scandir_ext((d),(f),(h),(a)) 264 #endif 265 266 void unix_path2dos_path( char *path ); 267 char *asim_find_file (const char *file, const char *pathlist, int type); 268 #define find_file(f,p,t) asim_find_file(f,p,t) 269 char *asim_copy_replace_envvar (char *path); 270 #define copy_replace_envvar(p) asim_copy_replace_envvar(p) 271 272 const char *asim_parse_argb_color( const char *color, CARD32 *pargb ); 273 #define parse_argb_color(c,p) asim_parse_argb_color((c),(p)) 274 275 double asim_parse_math(const char* str, char** endptr, double size); 276 #define parse_math(s,e,sz) asim_parse_math((s),(e),(sz)) 277 278 #ifdef __hpux 279 #define PORTABLE_SELECT(w,i,o,e,t) select((w),(int *)(i),(int *)(o),(e),(t)) 280 #else 281 #define PORTABLE_SELECT(w,i,o,e,t) select((w),(i),(o),(e),(t)) 282 #endif 283 284 /* from libAfterBase/socket.h : */ 285 #ifdef WORDS_BIGENDIAN 286 #define as_ntohl(ui32) (ui32) 287 #define as_hlton(ui32) (ui32) 288 #define as_ntohl16(ui16) (ui16) 289 #define as_hlton16(ui16) (ui16) 290 #else 291 #define as_ntohl(ui32) ((((ui32)&0x000000FF)<<24)|(((ui32)&0x0000FF00)<<8)|(((ui32)&0x00FF0000)>>8)|(((ui32)&0xFF000000)>>24)) 292 #define as_hlton(ui32) as_ntohl(ui32) /* conversion is symmetrical */ 293 #define as_ntohl16(ui16) ((((ui16)&0x00FF)<<8)|(((ui16)&0xFF00)>>8)) 294 #define as_hlton16(ui16) as_ntohl(ui16) /* conversion is symmetrical */ 295 #endif 296 297 #if 0 298 typedef union ASHashableValue 299 { 300 unsigned long long_val; 301 char *string_val; 302 void *ptr ; 303 } 304 ASHashableValue; 305 #else 306 typedef unsigned long ASHashableValue; 307 #endif 308 309 typedef union ASHashData 310 { 311 void *vptr ; 312 int *iptr ; 313 unsigned int *uiptr ; 314 long *lptr ; 315 unsigned long *ulptr ; 316 char *cptr ; 317 int i ; 318 unsigned int ui ; 319 long l ; 320 unsigned long ul ; 321 CARD32 c32 ; 322 CARD16 c16 ; 323 CARD8 c8 ; 324 }ASHashData; 325 326 #define AS_HASHABLE(v) ((ASHashableValue)((unsigned long)(v))) 327 328 typedef struct ASHashItem 329 { 330 struct ASHashItem *next; 331 ASHashableValue value; 332 void *data; /* optional data structure related to this 333 hashable value */ 334 } 335 ASHashItem; 336 337 typedef unsigned short ASHashKey; 338 typedef ASHashItem *ASHashBucket; 339 340 typedef struct ASHashTable 341 { 342 ASHashKey size; 343 ASHashBucket *buckets; 344 ASHashKey buckets_used; 345 unsigned long items_num; 346 347 ASHashItem *most_recent ; 348 349 ASHashKey (*hash_func) (ASHashableValue value, ASHashKey hash_size); 350 long (*compare_func) (ASHashableValue value1, ASHashableValue value2); 351 void (*item_destroy_func) (ASHashableValue value, void *data); 352 } 353 ASHashTable; 354 355 typedef enum 356 { 357 358 ASH_BadParameter = -3, 359 ASH_ItemNotExists = -2, 360 ASH_ItemExistsDiffer = -1, 361 ASH_ItemExistsSame = 0, 362 ASH_Success = 1 363 } 364 ASHashResult; 365 366 void asim_init_ashash (ASHashTable * hash, Bool freeresources); 367 ASHashTable *asim_create_ashash (ASHashKey size, 368 ASHashKey (*hash_func) (ASHashableValue, ASHashKey), 369 long (*compare_func) (ASHashableValue, ASHashableValue), 370 void (*item_destroy_func) (ASHashableValue, void *)); 371 void asim_destroy_ashash (ASHashTable ** hash); 372 ASHashResult asim_add_hash_item (ASHashTable * hash, ASHashableValue value, void *data); 373 ASHashResult asim_get_hash_item (ASHashTable * hash, ASHashableValue value, void **trg); 374 ASHashResult asim_remove_hash_item (ASHashTable * hash, ASHashableValue value, void **trg, Bool destroy); 375 376 void asim_flush_ashash_memory_pool(); 377 ASHashKey asim_string_hash_value (ASHashableValue value, ASHashKey hash_size); 378 long asim_string_compare (ASHashableValue value1, ASHashableValue value2); 379 void asim_string_destroy_without_data (ASHashableValue value, void *data); 380 /* variation for case-unsensitive strings */ 381 ASHashKey asim_casestring_hash_value (ASHashableValue value, ASHashKey hash_size); 382 long asim_casestring_compare (ASHashableValue value1, ASHashableValue value2); 383 384 ASHashKey asim_pointer_hash_value (ASHashableValue value, ASHashKey hash_size); 385 386 #define init_ashash(h,f) asim_init_ashash(h,f) 387 #define create_ashash(s,h,c,d) asim_create_ashash(s,h,c,d) 388 #define destroy_ashash(h) asim_destroy_ashash(h) 389 #define add_hash_item(h,v,d) asim_add_hash_item(h,v,d) 390 #define get_hash_item(h,v,t) asim_get_hash_item(h,v,t) 391 #define remove_hash_item(h,v,t,d) asim_remove_hash_item(h,v,t,d) 392 #define flush_ashash_memory_pool asim_flush_ashash_memory_pool 393 394 #define string_hash_value asim_string_hash_value 395 #define pointer_hash_value asim_pointer_hash_value 396 #define string_compare asim_string_compare 397 #define string_destroy_without_data asim_string_destroy_without_data 398 #define casestring_hash_value asim_casestring_hash_value 399 #define casestring_compare asim_casestring_compare 400 401 /* from sleep.c */ 402 void asim_start_ticker (unsigned int size); 403 void asim_wait_tick (); 404 #define start_ticker asim_start_ticker 405 #define wait_tick asim_wait_tick 406 407 /* TODO : add xml stuff */ 408 /* from xml.c */ 409 410 #define xml_tagchar(a) (isalnum(a) || (a) == '-' || (a) == '_') 411 412 #define XML_CDATA_STR "CDATA" 413 #define XML_CONTAINER_STR "CONTAINER" 414 #define XML_CDATA_ID -2 415 #define XML_CONTAINER_ID -1 416 #define XML_UNKNOWN_ID 0 417 418 #define IsCDATA(pe) ((pe) && (pe)->tag_id == XML_CDATA_ID) 419 420 typedef struct xml_elem_t { 421 struct xml_elem_t* next; 422 struct xml_elem_t* child; 423 char* tag; 424 int tag_id; 425 char* parm; 426 } xml_elem_t; 427 428 typedef enum 429 { 430 ASXML_Start = 0, 431 ASXML_TagOpen = 1, 432 ASXML_TagName = 2, 433 ASXML_TagAttrOrClose = 3, 434 ASXML_AttrName = 4, 435 ASXML_AttrEq = 5, 436 ASXML_AttrValueStart = 6, 437 ASXML_AttrValue = 7, 438 ASXML_AttrSlash = 8 439 } ASXML_ParserState; 440 441 typedef enum 442 { 443 ASXML_BadStart = -1, 444 ASXML_BadTagName = -2, 445 ASXML_UnexpectedSlash = -3, 446 ASXML_UnmatchedClose = -4, 447 ASXML_BadAttrName = -5, 448 ASXML_MissingAttrEq = -6 449 } ASXML_ParserError; 450 451 typedef struct ASXmlBuffer 452 { 453 char *buffer ; 454 int allocated, used, current ; 455 456 int state ; 457 int level ; 458 Bool verbatim; 459 Bool quoted; 460 461 enum 462 { 463 ASXML_OpeningTag = 0, 464 ASXML_SimpleTag, 465 ASXML_ClosingTag 466 }tag_type ; 467 468 int tags_count ; 469 }ASXmlBuffer; 470 471 void asim_asxml_var_insert(const char* name, int value); 472 int asim_asxml_var_get(const char* name); 473 void asim_asxml_var_init(void); 474 void asim_asxml_var_cleanup(void); 475 476 xml_elem_t* asim_xml_parse_parm(const char* parm, struct ASHashTable *vocabulary); 477 478 void asim_xml_elem_delete(xml_elem_t** list, xml_elem_t* elem); 479 xml_elem_t* asim_xml_parse_doc(const char* str, struct ASHashTable *vocabulary); 480 int asim_xml_parse(const char* str, xml_elem_t* current, struct ASHashTable *vocabulary); 481 482 void asim_reset_xml_buffer( ASXmlBuffer *xb ); 483 void asim_free_xml_buffer_resources (ASXmlBuffer *xb); 484 485 void asim_add_xml_buffer_chars( ASXmlBuffer *xb, char *tmp, int len ); 486 int asim_spool_xml_tag( ASXmlBuffer *xb, char *tmp, int len ); 487 488 Bool asim_xml_tags2xml_buffer (xml_elem_t *tags, ASXmlBuffer *xb, int tags_count, int depth); 489 void asim_xml_print (xml_elem_t* root); 490 xml_elem_t *asim_format_xml_buffer_state (ASXmlBuffer *xb); 491 492 char *asim_interpret_ctrl_codes( char *text ); 493 494 #define asxml_var_insert(n,v) asim_asxml_var_insert((n),(v)) 495 #define asxml_var_get(n) asim_asxml_var_get((n)) 496 #define asxml_var_init asim_asxml_var_init 497 #define asxml_var_cleanup asim_asxml_var_cleanup 498 499 #define xml_parse_parm(p,v) asim_xml_parse_parm((p),(v)) 500 501 #define xml_elem_delete(l,e) asim_xml_elem_delete((l),(e)) 502 #define xml_parse_doc(s,v) asim_xml_parse_doc((s),(v)) 503 #define xml_parse(s,c,v) asim_xml_parse((s),(c),(v)) 504 505 #define reset_xml_buffer(xb) asim_reset_xml_buffer((xb)) 506 #define free_xml_buffer_resources(xb) asim_free_xml_buffer_resources((xb)) 507 508 #define add_xml_buffer_chars(xb,t,l) asim_add_xml_buffer_chars((xb),(t),(l)) 509 #define spool_xml_tag(xb,t,l) asim_spool_xml_tag((xb),(t),(l)) 510 511 #define xml_tags2xml_buffer(t,xb,tc,d) asim_xml_tags2xml_buffer((t),(xb),(tc),(d)) 512 #define xml_print(r) asim_xml_print((r)) 513 #define format_xml_buffer_state(xb) asim_format_xml_buffer_state((xb)) 514 515 #define interpret_ctrl_codes(t) asim_interpret_ctrl_codes((t)) 516 517 #ifdef __cplusplus 518 } 519 #endif 520 521 522 #endif /* ASIM_AFTERBASE_H_HEADER_INCLUDED */ 523 524