common.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #if __GNUC__ >= 7
  49. # define FALLTHROUGH __attribute__((fallthrough));
  50. #else
  51. # define FALLTHROUGH
  52. #endif
  53. #ifdef __GNUC__
  54. # define INLINE __inline__
  55. #else
  56. # define INLINE
  57. #endif
  58. #define EXE "mbsync"
  59. /* main.c */
  60. #define DEBUG_CRASH 0x01
  61. #define DEBUG_MAILDIR 0x02
  62. #define DEBUG_NET 0x04
  63. #define DEBUG_NET_ALL 0x08
  64. #define DEBUG_SYNC 0x10
  65. #define DEBUG_MAIN 0x20
  66. #define DEBUG_DRV 0x40
  67. #define DEBUG_DRV_ALL 0x80
  68. #define DEBUG_ALL (0xFF & ~(DEBUG_NET_ALL | DEBUG_DRV_ALL))
  69. #define QUIET 0x100
  70. #define VERYQUIET 0x200
  71. #define PROGRESS 0x400
  72. #define VERBOSE 0x800
  73. #define KEEPJOURNAL 0x1000
  74. #define ZERODELAY 0x2000
  75. extern int DFlags;
  76. extern int JLimit;
  77. extern int UseFSync;
  78. extern char FieldDelimiter;
  79. extern int Pid;
  80. extern char Hostname[256];
  81. extern const char *Home;
  82. extern int BufferLimit;
  83. extern int new_total[2], new_done[2];
  84. extern int flags_total[2], flags_done[2];
  85. extern int trash_total[2], trash_done[2];
  86. void stats( void );
  87. /* util.c */
  88. void vdebug( int, const char *, va_list va );
  89. void vdebugn( int, const char *, va_list va );
  90. void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
  91. void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
  92. void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
  93. void ATTR_PRINTFLIKE(1, 2) notice( const char *, ... );
  94. void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
  95. void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
  96. void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
  97. void flushn( void );
  98. typedef struct string_list {
  99. struct string_list *next;
  100. char string[1];
  101. } ATTR_PACKED(void *) string_list_t;
  102. void add_string_list_n( string_list_t **list, const char *str, int len );
  103. void add_string_list( string_list_t **list, const char *str );
  104. void free_string_list( string_list_t *list );
  105. #ifndef HAVE_MEMRCHR
  106. void *memrchr( const void *s, int c, size_t n );
  107. #endif
  108. #ifndef HAVE_STRNLEN
  109. size_t strnlen( const char *str, size_t maxlen );
  110. #endif
  111. int starts_with( const char *str, int strl, const char *cmp, int cmpl );
  112. int starts_with_upper( const char *str, int strl, const char *cmp, int cmpl );
  113. int equals( const char *str, int strl, const char *cmp, int cmpl );
  114. #ifndef HAVE_TIMEGM
  115. time_t timegm( struct tm *tm );
  116. #endif
  117. void *nfmalloc( size_t sz );
  118. void *nfcalloc( size_t sz );
  119. void *nfrealloc( void *mem, size_t sz );
  120. char *nfstrndup( const char *str, size_t nchars );
  121. char *nfstrdup( const char *str );
  122. int nfvasprintf( char **str, const char *fmt, va_list va );
  123. int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
  124. int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, ... );
  125. void ATTR_NORETURN oob( void );
  126. char *expand_strdup( const char *s );
  127. int map_name( const char *arg, char **result, int reserve, const char *in, const char *out );
  128. #define DEFINE_ARRAY_TYPE(T) \
  129. typedef struct { \
  130. T *data; \
  131. int size; \
  132. } ATTR_PACKED(T *) T##_array_t; \
  133. typedef struct { \
  134. T##_array_t array; \
  135. int alloc; \
  136. } ATTR_PACKED(T *) T##_array_alloc_t; \
  137. static INLINE T *T##_array_append( T##_array_alloc_t *arr ) \
  138. { \
  139. if (arr->array.size == arr->alloc) { \
  140. arr->alloc = arr->alloc * 2 + 100; \
  141. arr->array.data = nfrealloc( arr->array.data, arr->alloc * sizeof(T) ); \
  142. } \
  143. return &arr->array.data[arr->array.size++]; \
  144. }
  145. #define ARRAY_INIT(arr) \
  146. do { (arr)->array.data = 0; (arr)->array.size = (arr)->alloc = 0; } while (0)
  147. #define ARRAY_SQUEEZE(arr) \
  148. do { \
  149. (arr)->data = nfrealloc( (arr)->data, (arr)->size * sizeof((arr)->data[0]) ); \
  150. } while (0)
  151. DEFINE_ARRAY_TYPE(uint)
  152. void sort_uint_array( uint_array_t array );
  153. int find_uint_array( const uint_array_t array, uint value );
  154. void arc4_init( void );
  155. uchar arc4_getbyte( void );
  156. int bucketsForSize( int size );
  157. typedef struct list_head {
  158. struct list_head *next, *prev;
  159. } list_head_t;
  160. typedef struct notifier {
  161. struct notifier *next;
  162. void (*cb)( int what, void *aux );
  163. void *aux;
  164. #ifdef HAVE_SYS_POLL_H
  165. int index;
  166. #else
  167. int fd, events;
  168. #endif
  169. } notifier_t;
  170. #ifdef HAVE_SYS_POLL_H
  171. # include <sys/poll.h>
  172. #else
  173. # define POLLIN 1
  174. # define POLLOUT 4
  175. # define POLLERR 8
  176. #endif
  177. void init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux );
  178. void conf_notifier( notifier_t *sn, int and_events, int or_events );
  179. void wipe_notifier( notifier_t *sn );
  180. typedef struct {
  181. list_head_t links;
  182. void (*cb)( void *aux );
  183. void *aux;
  184. time_t timeout;
  185. } wakeup_t;
  186. void init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux );
  187. void conf_wakeup( wakeup_t *tmr, int timeout );
  188. void wipe_wakeup( wakeup_t *tmr );
  189. static INLINE int pending_wakeup( wakeup_t *tmr ) { return tmr->links.next != 0; }
  190. void main_loop( void );
  191. #endif