common.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #ifndef COMMON_H
  23. #define COMMON_H
  24. #include <autodefs.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <time.h>
  29. typedef unsigned char uchar;
  30. typedef unsigned short ushort;
  31. typedef unsigned int uint;
  32. #define as(ar) (sizeof(ar)/sizeof(ar[0]))
  33. #define __stringify(x) #x
  34. #define stringify(x) __stringify(x)
  35. #define shifted_bit(in, from, to) \
  36. (((uint)(in) / (from > to ? from / to : 1) * (to > from ? to / from : 1)) & to)
  37. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  38. # define ATTR_UNUSED __attribute__((unused))
  39. # define ATTR_NORETURN __attribute__((noreturn))
  40. # define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
  41. # define ATTR_PACKED(ref) __attribute__((packed,aligned(sizeof(ref))))
  42. #else
  43. # define ATTR_UNUSED
  44. # define ATTR_NORETURN
  45. # define ATTR_PRINTFLIKE(fmt,var)
  46. # define ATTR_PACKED(ref)
  47. #endif
  48. #ifdef __GNUC__
  49. # define INLINE __inline__
  50. #else
  51. # define INLINE
  52. #endif
  53. #define EXE "mbsync"
  54. /* main.c */
  55. #define DEBUG_CRASH 0x01
  56. #define DEBUG_MAILDIR 0x02
  57. #define DEBUG_NET 0x04
  58. #define DEBUG_NET_ALL 0x08
  59. #define DEBUG_SYNC 0x10
  60. #define DEBUG_MAIN 0x20
  61. #define DEBUG_ALL (0xFF & ~DEBUG_NET_ALL)
  62. #define QUIET 0x100
  63. #define VERYQUIET 0x200
  64. #define PROGRESS 0x400
  65. #define VERBOSE 0x800
  66. #define KEEPJOURNAL 0x1000
  67. #define ZERODELAY 0x2000
  68. extern int DFlags;
  69. extern int UseFSync;
  70. extern char FieldDelimiter;
  71. extern int Pid;
  72. extern char Hostname[256];
  73. extern const char *Home;
  74. extern int BufferLimit;
  75. extern int new_total[2], new_done[2];
  76. extern int flags_total[2], flags_done[2];
  77. extern int trash_total[2], trash_done[2];
  78. void stats( void );
  79. /* util.c */
  80. void vdebug( int, const char *, va_list va );
  81. void vdebugn( int, const char *, va_list va );
  82. void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
  83. void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
  84. void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
  85. void ATTR_PRINTFLIKE(1, 2) notice( const char *, ... );
  86. void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
  87. void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
  88. void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
  89. void flushn( void );
  90. typedef struct string_list {
  91. struct string_list *next;
  92. char string[1];
  93. } ATTR_PACKED(void *) string_list_t;
  94. void add_string_list_n( string_list_t **list, const char *str, int len );
  95. void add_string_list( string_list_t **list, const char *str );
  96. void free_string_list( string_list_t *list );
  97. #ifndef HAVE_MEMRCHR
  98. void *memrchr( const void *s, int c, size_t n );
  99. #endif
  100. #ifndef HAVE_STRNLEN
  101. size_t strnlen( const char *str, size_t maxlen );
  102. #endif
  103. int starts_with( const char *str, int strl, const char *cmp, int cmpl );
  104. int starts_with_upper( const char *str, int strl, const char *cmp, int cmpl );
  105. int equals( const char *str, int strl, const char *cmp, int cmpl );
  106. #ifndef HAVE_TIMEGM
  107. time_t timegm( struct tm *tm );
  108. #endif
  109. void *nfmalloc( size_t sz );
  110. void *nfcalloc( size_t sz );
  111. void *nfrealloc( void *mem, size_t sz );
  112. char *nfstrndup( const char *str, size_t nchars );
  113. char *nfstrdup( const char *str );
  114. int nfvasprintf( char **str, const char *fmt, va_list va );
  115. int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
  116. int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, ... );
  117. void ATTR_NORETURN oob( void );
  118. char *expand_strdup( const char *s );
  119. int map_name( const char *arg, char **result, int reserve, const char *in, const char *out );
  120. #define DEFINE_ARRAY_TYPE(T) \
  121. typedef struct { \
  122. T *data; \
  123. int size; \
  124. } ATTR_PACKED(T *) T##_array_t; \
  125. typedef struct { \
  126. T##_array_t array; \
  127. int alloc; \
  128. } ATTR_PACKED(T *) T##_array_alloc_t; \
  129. static INLINE T *T##_array_append( T##_array_alloc_t *arr ) \
  130. { \
  131. if (arr->array.size == arr->alloc) { \
  132. arr->alloc = arr->alloc * 2 + 100; \
  133. arr->array.data = nfrealloc( arr->array.data, arr->alloc * sizeof(T) ); \
  134. } \
  135. return &arr->array.data[arr->array.size++]; \
  136. }
  137. #define ARRAY_INIT(arr) \
  138. do { (arr)->array.data = 0; (arr)->array.size = (arr)->alloc = 0; } while (0)
  139. #define ARRAY_SQUEEZE(arr) \
  140. do { \
  141. (arr)->data = nfrealloc( (arr)->data, (arr)->size * sizeof((arr)->data[0]) ); \
  142. } while (0)
  143. DEFINE_ARRAY_TYPE(int)
  144. void sort_int_array( int_array_t array );
  145. int find_int_array( const int_array_t array, int value );
  146. void arc4_init( void );
  147. uchar arc4_getbyte( void );
  148. int bucketsForSize( int size );
  149. typedef struct list_head {
  150. struct list_head *next, *prev;
  151. } list_head_t;
  152. typedef struct notifier {
  153. struct notifier *next;
  154. void (*cb)( int what, void *aux );
  155. void *aux;
  156. #ifdef HAVE_SYS_POLL_H
  157. int index;
  158. #else
  159. int fd, events;
  160. #endif
  161. } notifier_t;
  162. #ifdef HAVE_SYS_POLL_H
  163. # include <sys/poll.h>
  164. #else
  165. # define POLLIN 1
  166. # define POLLOUT 4
  167. # define POLLERR 8
  168. #endif
  169. void init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux );
  170. void conf_notifier( notifier_t *sn, int and_events, int or_events );
  171. void wipe_notifier( notifier_t *sn );
  172. typedef struct {
  173. list_head_t links;
  174. void (*cb)( void *aux );
  175. void *aux;
  176. time_t timeout;
  177. } wakeup_t;
  178. void init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux );
  179. void conf_wakeup( wakeup_t *tmr, int timeout );
  180. void wipe_wakeup( wakeup_t *tmr );
  181. static INLINE int pending_wakeup( wakeup_t *tmr ) { return tmr->links.next != 0; }
  182. void main_loop( void );
  183. #endif